title: Spring Boot Could not find acceptable representation for XML responses
body: <p>I found multiple solutions for this exception and causes but none worked and it seems illogical.</p>
  
  <p>I have REST service class as the following</p>
  
  <pre><code>import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.*;
  import org.springframework.http.MediaType;
  
  import java.util.Map;
  @RestController
  public class UserRestService {
  
      @RequestMapping(value = "/userLogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_XML_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
      public @ResponseBody ResponseEntity&lt;Map&gt; login(@RequestBody Map&lt;String, String&gt; name) {
          ResponseEntity&lt;Map&gt; res = null;
          name.values().forEach(System.out::println);
          return new ResponseEntity&lt;Map&gt;(name, HttpStatus.OK);
      }
  }
  </code></pre>
  
  <p>As you see, I try to create a POST request with request body as JSON and response as XML, printing the request in the process. It's just simple example but while the request is printed correctly which means it's been received and marshaled to Map. However the response status is 406 (not acceptable)</p>
  
  <p>Exception in the log is</p>
  
  <pre><code>John
  30
  null
  2018-08-16 14:05:48.440  WARN 22820 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
  2018-08-16 14:05:48.481  WARN 22820 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
  </code></pre>
  
  <p>the Request Header simply has</p>
  
  <pre><code>Accept:*/*
  Content-Type:application/json
  </code></pre>
  
  <p>POM.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.example.cms&lt;/groupId&gt;
      &lt;artifactId&gt;business&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.0.4.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
              &lt;version&gt;2.9.6&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
              &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
              &lt;version&gt;2.3.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
              &lt;artifactId&gt;jaxb-core&lt;/artifactId&gt;
              &lt;version&gt;2.3.0.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
              &lt;artifactId&gt;jaxb-impl&lt;/artifactId&gt;
              &lt;version&gt;2.3.0.1&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Noting that adding the following dependency solves the problem</p>
  
  <pre><code>&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;
  </code></pre>
  
  <p>Request Body <code>{ "name":"John", "age":30, "car":null }</code></p>
  
  <p>Response if <code>MediaType.APPLICATION_JSON_UTF8_VALUE</code> is used for <code>produces</code> attribute of <code>@RequestMapping</code> is <code>{ "name":"John", "age":30, "car":null }</code></p>
  
  <p>Response if <code>MediaType.APPLICATION_XML_VALUE</code> is used for <code>produces</code> attribute of <code>@RequestMapping</code> &amp; <code>jackson-dataformat-xml</code> used is</p>
  
  <pre><code>&lt;Map&gt;
      &lt;name&gt;John&lt;/name&gt;
      &lt;age&gt;30&lt;/age&gt;
      &lt;car/&gt;
  &lt;/Map&gt;
  </code></pre>
  
  <p>Why using JAXB fails in this ?</p>
  
  <h3><strong>Update 01</strong></h3>
  
  <p>OK as @Simon comment, I created a sample object (class) to get as request and response with it. <strong>Same exact effect</strong>. Here is the updated code</p>
  
  <pre><code>public class MockupResponse {
  
      private String name;
      private Integer age;
      private String car;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public Integer getAge() {
          return age;
      }
  
      public void setAge(Integer age) {
          this.age = age;
      }
  
      public String getCar() {
          return car;
      }
  
      public void setCar(String car) {
          this.car = car;
      }
  
  @Override
  public String toString() {
          StringBuffer sb = new StringBuffer();
          if (null != name)
              sb.append("Name: " + name + "\n");
          if (null != age)
              sb.append("Age: " + age + "\n");
          if (null != car)
              sb.append("Car: " + car + "\n");
  
          return sb.toString();
  }
  }
  </code></pre>
  
  <p>Service</p>
  
  <pre><code>@RequestMapping(value = "/userLogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
      public @ResponseBody ResponseEntity&lt;MockupResponse&gt; login(@RequestBody MockupResponse sample) {
          System.out.println(sample);
  
          return new ResponseEntity&lt;MockupResponse&gt;(sample, HttpStatus.OK);
      }
  </code></pre>