I have a requirement where I want to handle how exceptions are returned. Depending on the type of the controller method if it is annotated with @ResponseBody a json string should be returned. Additionally if it is a String returning method a jsp error page should be returned.
However it seems that I am unable to define two global exception handler (with ControllerAdvice) both which handles Exception.class but one returns a ModelAndView and the other is annotated with @ResponseBody. I get an exception mentioning that it is too ambigious.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class java.lang.Exception]
The below code is an example of an ideal situation where both scenarios are handled
The methods
@RequestMapping(value = "/{pathValue}/page", method = RequestMethod.GET)
public String getPage(@PathVariable(value="pathValue") String pathValue, ModelMap model) throws Exception {
@RequestMapping(value = "{pathValue}/jsonData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ModelMap getJson(@PathVariable(value="pathValue") String pathValue) throws Exception {
The Exception Handlers
@ExceptionHandler(Exception.class)
@ResponseBody ErrorInfo handleBadRequest(HttpServletRequest req, Exception ex) {
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
in the above example the getPage method should be handled by handleBadRequest and getJson should be handled by defaultErrorHandler
Is there any way to configure two global exception handlers which both handle the same Exception class and returns a page or json based on the Controller method type.