title: Return content type for Error Responses based on produces condition of @RequestMapping
body: <p>I have custom errors handling in my REST webservice. I have methods returning XML / JSON as response. Everything worked fine on SpringBoot version <strong>2.0.9</strong>. But after migration to latest (<strong>2.2.4</strong>) my error handling tests fail:</p>
  
  <pre><code>Content type expected:&lt;application/xml&gt; but was:&lt;application/json&gt;
  Expected :application/xml
  Actual   :application/json
  </code></pre>
  
  <p>After research I found that it's related with <strong>upgrading Spring to Version 5.1</strong>. Docs:</p>
  
  <blockquote>
    <p>Content Negotiation for Error Responses
    The produces condition of an @RequestMapping no longer impacts the content type of error responses.</p>
  </blockquote>
  
  <p>How can I reproduce behavior from earlier versions of Spring in the latest? I just want to return the content type for errors as specified in <strong>produces</strong> condition.</p>
  
  <p>Rest methods:</p>
  
  <pre><code>@PostMapping(path = "/{scriptName}", produces = { MediaType.APPLICATION_XML_VALUE })
  public ResponseEntity&lt;Object&gt; xmlMethod(@RequestParam("payload") String payload, @PathVariable("scriptName") String scriptName) {
  
      Object result = service.call(payload, scriptName);
      return ResponseEntity.ok(new JsonBuilder(result).getContent());
  }
  
  @PostMapping(path = "/{scriptName}", produces = { MediaType.APPLICATION_JSON_VALUE })
  public ResponseEntity&lt;Object&gt; jsonMethod(@RequestParam("payload") String payload, @PathVariable("scriptName") String scriptName) {
      Object result = service.call(payload, scriptName);
      return ResponseEntity.ok(new JsonBuilder(result).getContent());
  }
  </code></pre>
  
  <p>CustomRestExceptionHandler:</p>
  
  <pre><code>@ControllerAdvice
  public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler {
  
      @ExceptionHandler(ResourceNotFoundException.class)
      public ResponseEntity&lt;Object&gt; handleResourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
  
          HTTPErrorDTO httpError = new HTTPErrorDTO(HttpStatus.NOT_FOUND, ex.getLocalizedMessage());
          return new ResponseEntity&lt;&gt;(httpError, new HttpHeaders(), httpError.getStatus());
      }
  
      ....
      // handlers for other exceptions
  }
  </code></pre>
  
  <p>Error DTO:</p>
  
  <pre><code>@XmlRootElement(name = "Exception")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class HTTPErrorDTO {
  
      private HttpStatus status;
      private String message;
      private List&lt;String&gt; errors;
  }
  </code></pre>
  
  <p>Related topic: <a href="https://stackoverflow.com/questions/40971714/spring-mvc-configuring-error-handling-for-xml-and-json-response">Spring mvc - Configuring Error handling for XML and JSON Response</a></p>
  
  <p><strong>---EDIT</strong></p>
  
  <p>I tried to add custom content negotiation configuration. But one of clients of my REST API send me content-type = <strong>"application/x-www-form-urlencoded"</strong> and expect application/xml and don't send any accept headers.</p>
  
  <p>So I can only decide what format the content type should be at the method/controller level.</p>
  
  <p>Can I somehow inform exception handler from controller, which one
  content type should be set?</p>