1

I was hunting down a 400 BAD_REQUEST error in a controller method like:

@RequestMapping(value = "/{schedulerName}/plans", method = RequestMethod.POST, produces = "application/json", consumes = "application/json") @ResponseBody public PlanTemplate createPlanForScheduler(@RequestBody PlanTemplate planTemplate, @PathVariable String schedulerName) { ... } 

The catch is there was no error written on console. I figured it out at last by setting the root logger to DEBUG level.

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"]) 

As you can see, there is a HttpMessageNotReadableException error thrown, but ExceptionHandlerExceptionResolver sees this as a debugging information. Isn't it a little weird to conceal such an error?

What's the general approach here? I'm thinking of setting org.springframework.web.servlet.mvc to DEBUG, but it stil dumps to much information for my taste.

1 Answer 1

1

What about handling such cases in the controller. Spring provides annotations for that as follows. Then you can log it like that.

@ExceptionHandler(HttpMessageNotReadableException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) public void handleException(HttpMessageNotReadableException ex, HttpServletResponse response) { logger.info("Handling " + ex.getClass().getSimpleName()); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Ok. But I try to understand the rational behind hiding these exceptions on debug level loggers. To me, these seem like an exception exception, not something to conceal under the carpet.
I don't know why Spring is configured this way, but these Exceptions really get lost as the only way is to write custom handlers or configure custom HandlerExceptionResolver. It would be simple custom class that implements that resolver interface. Here's a link

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.