title: How to enable multiple Content-Types for a response in spring-mvc?
body: <p>I have a simple <code>@RestController</code> and would like to response with both <code>JSON</code> or <code>XML</code> depending on the http header <code>content-type</code>.</p>
  
  <p>Problem: I'm always only getting <code>XML</code> response, never JSON. Of course I'm using <code>Content-Type: application/json</code> as http header.</p>
  
  <p>What might be missing in the following configuration?</p>
  
  <pre><code>@RestController
  public void MyServlet {
  
      @RequestMapping(value = "test", method = RequestMethod.GET,
              produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
      public MyResponse test() {
          return new MyResponse();
      }
  }
  
  @XmlRootElement
  public class MyResponse {
      private String test = "somevalue";
      //getter, setter
  }
  </code></pre>
  
  <p>pom.xml:</p>
  
  <pre><code>       &lt;!-- as advised in: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.dataformat&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-dataformat-xml&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.codehaus.woodstox&lt;/groupId&gt;
              &lt;artifactId&gt;woodstox-core-asl&lt;/artifactId&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>Interestingly: if I switch the produces statement:
  <code>produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})</code>,
  then I'm always getting <code>JSON</code> out and never <code>XML</code>!</p>
  
  <p>So the question is: why does the first MediaType always have precedence, and the http header is never taken into account?</p>