title: Multiple exception handlers for ResponseBody methods and normal methods
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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]</p>
  
  <p>The below code is an example of an ideal situation where both scenarios are handled</p>
  
  <p>The methods</p>
  
  <pre><code>@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 { 
  </code></pre>
  
  <p>The Exception Handlers</p>
  
  <pre><code>@ExceptionHandler(Exception.class)
  @ResponseBody ErrorInfo handleBadRequest(HttpServletRequest req, Exception ex) {
  
  @ExceptionHandler(value = Exception.class)
  public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { 
  </code></pre>
  
  <p>in the above example the getPage method should be handled by handleBadRequest and getJson should be handled by defaultErrorHandler</p>
  
  <p>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.</p>