Spring @ControllerAdvice ExceptionHandler based on content type

503 views Asked by At

We have a need to support both html and json responses in all our end points.

The html will be used by our own front-end application and json will be used by third-party API integrations.

We have used ContentNegotiatingViewResolver to use JstlView and MappingJackson2JsonView for html and json content respectively and that works well. With a single controller method, we get the html / json output based on url extension.

We want to have a similar technique for a global ExceptionHandler through @ControllerAdvice / @ExceptionHandler. But this does not seem to take into consideration, the content type, before resolving into a view.

  • How can I make the ExceptionHandler to pick the correct view to produce html or json based on url.extension ? (Note: I do not want to use @ResponseBody or ResponseEntity since I need a ModelAndView to output a html based error page)

  • Can I create a Custom View Resolver to do this for ExceptionHandling and how do I stitch it in the Spring Configuration, along side my ContentNegotiationViewResolver used for normal responses ?

0

There are 0 answers