我在控制器方法中搜索400 BAD_REQUEST
错误,如:
@RequestMapping(value = "/{schedulerName}/plans", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
@ResponseBody
public PlanTemplate createPlanForScheduler(@RequestBody PlanTemplate planTemplate, @PathVariable String schedulerName) {
...
}
问题是控制台上没有写入错误。我最后通过将根记录器设置为DEBUG
级别来解决这个问题。
15:56:17.528 [1735537490@qtp-2089431280-0] 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 [1735537490@qtp-2089431280-0] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Reading [class sch.model.PlanTemplate] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@25d08402]
15:56:17.530 [1735537490@qtp-2089431280-0] 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,但是根据我的喜好,它会转储到很多信息。
答案 0 :(得分:1)
如何在控制器中处理此类情况。 Spring为此提供了如下注释。然后你可以像那样记录它。
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public void handleException(HttpMessageNotReadableException ex,
HttpServletResponse response) {
logger.info("Handling " + ex.getClass().getSimpleName());
}