2014-01-25 35 views
1

我是在一個控制器方法追捕一個400 BAD_REQUEST錯誤,如隱藏。我最終通過將根記錄器設置爲DEBUG的等級來計算出來。Spring MVC的異常是由解析器

15:56:17.528 [[email protected]] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public sch.model.PlanTemplate sch.controller.api.SchedulerController.createPlanForScheduler(sch.model.PlanTemplate,java.lang.String)] 
15:56:17.529 [[email protected]] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Reading [class sch.model.PlanTemplate] as "application/json" using [org.springfr[email protected]25d08402] 
15:56:17.530 [[email protected]] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public sch.model.PlanTemplate sch.controller.api.SchedulerController.createPlanForScheduler(sch.model.PlanTemplate,java.lang.String)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "schedulerExpression" (class sch.model.PlanTemplate), not marked as ignorable (9 known properties: "name", "waitPrevious", "scheduleExpression", "averageDuration", "status", "parallelLimit", "id", "scheduler", "description"]) 

正如你可以看到,有一個HttpMessageNotReadableException錯誤拋出,但ExceptionHandlerExceptionResolver認爲這是一個調試信息。隱瞞這樣的錯誤不是有點奇怪嗎?

這裏的一般方法是什麼?我正在考慮將org.springframework.web.servlet.mvc設置爲DEBUG,但它爲我的口味留下了很多信息。

回答

1

如何處理控制器中的這種情況。 Spring爲此提供了註釋,如下所示。然後你可以這樣記錄它。

@ExceptionHandler(HttpMessageNotReadableException.class) 
@ResponseStatus(value = HttpStatus.BAD_REQUEST) 
public void handleException(HttpMessageNotReadableException ex, 
    HttpServletResponse response) { 

    logger.info("Handling " + ex.getClass().getSimpleName()); 
} 
+0

好的。但我試圖理解在調試級別記錄器上隱藏這些異常的理由。對我而言,這些看起來像是個例外,不是在地毯下隱藏的東西。 – SelimOber

+0

我不知道爲什麼Spring以這種方式配置,但是這些異常真的會丟失,因爲唯一的方法是編寫自定義處理程序或配置自定義HandlerExceptionResolver。這將是實現該解析器接口的簡單自定義類。這是[鏈接](http://steveliles.github.io/configuring_global_exception_handling_in_spring_mvc.html) – Vaelyr