title: Spring MVC - @ExceptionHandler based on Accept header
body: <p>I have a <code>HandlerInterceptorAdapter</code> that intercepts all requests and performs user authorization checks. Very basically:</p>
  
  <pre><code>@Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      User user = ... // get user
      checkIfAuthorized(user); // throws AuthorizationException
      return true;
  }
  </code></pre>
  
  <p>I then have an <code>@ExceptionHandler</code> for that <code>AuthorizationException</code>.</p>
  
  <pre><code>@ExceptionHandler(value = AuthorizationException.class) 
  public ResponseEntity&lt;String&gt; handleNotAuthorized(AuthorizationException e) {
      // TODO Custom EXCEPTION HANDLER for json/jsp/xml/other types, based on content type
      ResponseEntity&lt;String&gt; responseEntity = new ResponseEntity&lt;&gt;("You are not authorized to access that page.", HttpStatus.UNAUTHORIZED);
      return responseEntity;
  }
  </code></pre>
  
  <p>This is fine if the (unauthorized) request accepts <code>text/plain</code> (and can be easily changed for json). 
  How can I make different <code>@ExceptionHandler</code>s for specific <code>Accept</code> headers?</p>
  
  <p><code>@RequestMapping</code> has <code>produces()</code>. Is there something similar for <code>@ExceptionHandler</code>?</p>