title: Spring MVC, Migrate @ExceptionHandler to HandlerExceptionResolver for a RESTful service
body: <h2>Background</h2>
  <p>I have an error message class:</p>
  <pre><code>@XmlRootElement
  public class ErrorMessage {
      private String message;
  
      public ErrorMessage() {
      }
  
      public ErrorMessage(String message) {
          this.message = message;
      }
  
      public String getError() {
          return message;
      }
  
      public void setError(String message) {
          this.message = message;
      }
  }
  </code></pre>
  <br/>
  <p>This class has been assigned as a return value to an @ExceptionHandler in my Spring MVC REST controller:</p>
  <pre><code>@ExceptionHandler
  @ResponseStatus(HttpStatus.NOT_FOUND)
  @ResponseBody
  ErrorMessage handleException(RuntimeException e) {
      return new ErrorMessage(&quot;something went wrong&quot;);
  }
  </code></pre>
  <p>Whenever the client triggers a <code>RuntimeException</code> after issuing a request with <code>application/json</code> as a <code>Accept</code> header, it receives a response with the correct status code and a matching JSON body:</p>
  <pre><code>{&quot;error&quot;:&quot;something went wrong&quot;}
  </code></pre>
  <p>Alternatively, an XML body is received if the <code>Accept</code> header is <code>application/xml</code>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
  &lt;errorMessage&gt;&lt;error&gt;something went wrong&lt;/error&gt;&lt;/errorMessage&gt;
  </code></pre>
  <br/>
  <h2>Problem</h2>
  <p>Now, I would like to generify this solution by implementing a <a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-exceptionhandlers" rel="noreferrer">HandlerExceptionResolver</a> instead so I don't have to copy / paste the <code>@ExceptionHandler</code> to every controller (or create a common &quot;parent controller&quot; that the other controllers can extend).</p>
  <p>However, the <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/handler/AbstractHandlerExceptionResolver.html#doResolveException%28javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse,%20java.lang.Object,%20java.lang.Exception%29" rel="noreferrer">AbstractHandlerExceptionResolver.doResolveException()</a> method returns a <code>ModelAndView</code> and not my propriety <code>ErrorMessage</code>, so I tried the following:</p>
  <pre><code>public class RuntimeExceptionHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
  
      @Override
      protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
          if (ex instanceof RuntimeException) {
              response.setStatus(HttpServletResponse.SC_NOT_FOUND);
              ModelAndView mav = new ModelAndView();
              mav.addObject(&quot;error&quot;, &quot;something went wrong&quot;);
              return mav;
          }
          return null;
      }
  }
  </code></pre>
  <p>When debugging, I can see that the <code>mav.addObject()</code> method is called. The response on the client side has the expected status code, but the content type is <code>text/html</code> with html in the body, rather than the JSON or XLM content that was specified by the <code>Accept</code> header in the original request.</p>
  <p>(Side note, the actual exception, response code and text message in the example above is not important, they just serve as simple example.)</p>
  <p>Spring version: 3.1.1.RELEASE</p>