I have a restful controller where I return JSON to the client. My goal is to be able to handle exceptions by returning JSON when they occur.
I read about the ExceptionHandlerExceptionResolver, but failed while trying to get it to work. I have added an ExceptionHandlerExceptionResolver in my configuration, but it seems all exceptions are handled by another HandlerExceptionResolver I have implemented.
I'm not using <mvc:annotation-driven />, but have configured an AnnotationMethodHandlerAdapter with an MappingJacksonHttpMessageConverter amongst other stuff.
Is it a requirement to use ExceptionHandlerExceptionResolver with RequestMappingHandlerAdapter?
I'm missing a part of the big picture here..
I know I can implement a HandlerException resolver which returns for instance an MappingJacksonJsonView, but I find the above approach nicer...if I can make it workCode:@ControllerAdvice public class RestResponseEntityExceptionHandler { @ExceptionHandler(SomeException.class) protected ResponseEntity<Error> handleSomeException(SomeException ex) { Error restError = buildError(ex , HttpStatus.SERVICE_UNAVAILABLE); return new ResponseEntity<RestError>(restError, HttpStatus.SERVICE_UNAVAILABLE); } ... }![]()



Reply With Quote