Spring3 @ExceptionHandler for ServletRequestBindingException

I use the default AnnotationMethodHandlerAdapter parameter, which I believe should support @ExceptionHandler support. Unfortunately, a ServletRequestBindingException is thrown if a handler method call like the one below is called, rather than an Exception handler.

@RequestMapping(value = "/v1/products/{code}", method = RequestMethod.GET, headers = "Accept=application/xml,application/json")
@ResponseBody
public ProductDemoDTO getProductByCode(@PathVariable final String code,
        @RequestParam(required = false, defaultValue = "BASIC") final String options)
{
    //omitted
}

In this case, the ExceptionHandler never raised:

@ExceptionHandler(Throwable.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
//TODO not being called?
public void handleException(final Exception e, final HttpServletRequest request, final Writer writer) throws IOException
{
    writer.write(String.format("{\"error\":{\"java.class\":\"%s\", \"message\":\"%s\"}}", e.getClass(), e.getMessage()));
}

Does anyone know why the ExceptionHandler is not called?

+4
source share
4 answers

you cannot handle it with spring custom implementation.

, web.xml <error-page>. .

+1

​​ Spring 3.2. @ControllerAdvice. @ExceptionHandler ServletRequestBindingException . :

@ControllerAdvice
public class GlobalExceptionHandler {
  @ExceptionHandler(ServletRequestBindingException.class)
  public ResponseEntity<String> handleServletRequestBindingException(ServletRequestBindingException ex)   {
      return new ResponseEntity<String>("MISSING REQUIRED HEADER",HttpStatus.PRECONDITION_REQUIRED);
  }
}

Spring mvc docs: 17.11

+3

, @ExceptionHandler , . ServletRequestBindingException - , , - , @ExceptionHandler .

, , . , ServletRequestBindingException, .

+2

Aaand Juergen Hoeller, Spring 4.3.

, : https://jira.spring.io/browse/SPR-11106

0

All Articles