title: Spring HttpMessageConverter according to @ExceptionHandler response content type
body: <p>I have a Rest web service based on <strong>Spring MVC</strong>. I use a <code>@RestControllerAdvice</code> to handle exceptions thrown from my <code>@Controller</code>s. </p>
  
  <hr>
  
  <h2>Controller</h2>
  
  <p>An example of call is as below </p>
  
  <pre><code>@GetMapping(value = "/{id}/{name:.+}", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE,
          MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE })
  ResponseEntity&lt;byte[]&gt; getSomething(
          @PathVariable("id") String id, @PathVariable("name") String name) throws customException;
  </code></pre>
  
  <p><em>A basic method that can produce 3 media types :  <code>APPLICATION_OCTET_STREAM_VALUE</code>, <code>APPLICATION_XML_VALUE</code> and <code>APPLICATION_JSON_VALUE</code> and throws a <code>customException</code></em></p>
  
  <hr>
  
  <h2>Exception handler</h2>
  
  <p>The definition of my <code>@RestControllerAdvice</code> is as the following :</p>
  
  <pre><code>@ExceptionHandler({ CustomException.class })
  public ResponseEntity&lt;Object&gt; handleException(CustomException e) {
  
      ErrorDto err = errorMapper.map(e);
      Enumeration&lt;String&gt; en = httpServletRequest.getHeaders(HttpHeaders.ACCEPT);
  
      while (en.hasMoreElements()) {
          String list = en.nextElement();
          StringTokenizer st = new StringTokenizer(list, ",");
  
          while (st.hasMoreTokens()) {
  
              String acc = st.nextToken();
              MediaType contentTypeHeader = MediaType.valueOf(acc);
  
              if (MediaType.APPLICATION_XML.includes(contentTypeHeader)) {
  
                  JAXBElement&lt;ErrorDto&gt; ret = new ObjectFactory().createError(err);
                  return ResponseEntity.status(HttpStatus.PRECONDITION_FAILED)
                          .contentType(MediaType.APPLICATION_XML).body(ret);
  
              } else if (MediaType.APPLICATION_JSON.includes(contentTypeHeader)) {
  
                  return ResponseEntity.status(HttpStatus.PRECONDITION_FAILED)
                          .contentType(MediaType.APPLICATION_JSON).body(err);
  
              }
          }
      }
  
      return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(null);
  }
  </code></pre>
  
  <p><em>According to the request accept header, the <code>@ExceptionHandler</code> returns a null body and a <code>NOT_ACCEPTABLE</code> http response status if no accept is set, or a object of type <code>ErrorDto</code> if accept is of type <code>APPLICATION_JSON</code> or a <code>JaxbElement</code> of <code>ErrorDto</code> is accept is of type <code>APPLICATION_XML</code></em></p>
  
  <blockquote>
    <p>Please note that I specify the content type of the response when it
    contains a body.</p>
  </blockquote>
  
  <hr>
  
  <h2>Problem</h2>
  
  <p>My problem is when a client make a call with multiple accept headers, Spring tries to pick up an <code>HttpMessageConverter</code> according to the accept header and not to the response content type. Below an example :</p>
  
  <p>When the client call a method that throws an exception (and then returns an errorDto) with multiple Accept headers as below :</p>
  
  <pre><code>HttpHeaders headers = new HttpHeaders();
  headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_VALUE);
  headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_VALUE);
  HttpEntity&lt;?&gt; entity = new HttpEntity&lt;&gt;(body, headers);
  
  restTemplate.exchange(uriComponents.encode().toUri(), httpMethod, entity, Class);
  </code></pre>
  
  <p>Spring does not return a response with the error in the XML format as expected. It looks for a <code>Octet-Stream &lt;-&gt; JaxbElement</code> converter that does not exist and does not try to find the converter according to the response content type.</p>
  
  <h2>How can I force Spring to use a converter according to the response content type ?</h2>
  
  <hr>
  
  <p>I'm using :</p>
  
  <ul>
  <li><strong>Spring Boot 1.4.0.RELEASE</strong></li>
  </ul>