title: Spring MVC - How to set return media type when handling HttpMediaTypeNotAcceptableException?
body: <p>I have a Spring 3.2 MVC REST service, which implements error handling by extending <code>ResponseEntityExceptionHandler</code>. This handles the <a href="http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-rest-spring-mvc-exceptions" rel="nofollow">standard Spring exceptions</a>, and will respond with the appropriate HTTP status and custom "Error" ResponseEntity object in XML or JSON as requested by the client. This works great for all of the standard exceptions except <code>HttpMediaTypeNotAcceptableException</code>. </p>
  
  <p>The problem I have with this exception is that the reason its thrown in the first place is because the response media type (application/xml, application/json, etc.) could not be determined from the request. If I try to return an "Error" ResponseEntity object for this exception it will fail because the response media type couldn't be determined (which is why this exception is being handled in the first place)...and basically get another <code>HttpMediaTypeNotAcceptableException</code> thrown from my ExceptionHandler. </p>
  
  <p>I need to figure out a way when handling <code>HttpMediaTypeNotAcceptableException</code> to specify a valid response media type, so the ResponseEntity makes it to the client. Since requested media type can't be determined, this would probably just be the default for my service (application/xml)</p>
  
  <p>Any ideas?</p>