title: REST api returning json but configured as XML?
body: <p>I'm trying to get an XML response from my server but for some reason it still returns JSON. Maybe I'm misunderstanding what setAccept and setContentType are? At the moment I'm thinking that the get request is in XML but the client is only accepting / expecting json? </p>
  
  <p>I've tried setting headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); to XML instead but it ends up giving me an error "Cannot find representation". I also took out JSON from the consumes = "" but it stopped working when i did that too.</p>
  
  <p>RestClient.java</p>
  
  <pre><code>public class RestClient {
  
       public static void getJsonEmployee(String id) throws JSONException, IOException {
  
            String uri = "http://localhost:8080/employees/" + id;
  
               RestTemplate restTemplate = new RestTemplate();
  
               HttpHeaders headers = new HttpHeaders();
               headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
               headers.setContentType((MediaType.APPLICATION_XML));
               HttpEntity entity = new HttpEntity(headers);
  
  
              ResponseEntity&lt;String&gt; response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
               String result = response.getBody();
               System.out.println(result);
  
           }
  
  
  
  
  public static void postJsonEmployee(String id, String name, String description) {
  
      final String uri = "http://localhost:8080/employees/";
  
  
      Employee newemp = new Employee(id, name, description);
  
  
      RestTemplate restTemplate = new RestTemplate();
  
  
      HttpHeaders httpHeaders = restTemplate.headForHeaders(uri);
  
  
      httpHeaders.setContentType(MediaType.APPLICATION_JSON);
  
  
      Employee result = restTemplate.postForObject( uri, newemp, Employee.class);
  
  
      httpHeaders.setContentType(MediaType.APPLICATION_JSON);
  
  
       }
  
      public static void main(String[] args) throws IOException, JSONException {
  
       System.out.println("GET or POST?");
       BufferedReader getpost = new BufferedReader(new InputStreamReader(System.in));
       String selection = getpost.readLine();
  
       switch(selection) {
  
       case "GET":
  
       System.out.println("Type in the employee's ID");
       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
       String employeeid = reader.readLine();
       getJsonEmployee(employeeid);
       break;
  
       case "POST":
  
           System.out.println("Type in the employee's ID");
           Scanner scan = new Scanner(System.in);
           String newid = scan.nextLine();
           System.out.println("Type in the employee's name");
           String newname = scan.nextLine();
           System.out.println("Type in the employee's description");
           String newdesc = scan.nextLine();
           postJsonEmployee(newid, newname, newdesc);
           break;
       }
  
  }
  }
  
  </code></pre>
  
  <p>EmployeeController.java</p>
  
  <pre><code>@RestController
  public class EmployeeController {
  
      @Autowired
      private EmployeeService employeeService;
  
      @RequestMapping(path = "/employees", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_PLAIN_VALUE})
      public @ResponseBody HashMap&lt;String, Employee&gt; retrieveEmployees() {
          return employeeService.retrieveAllEmployees();
      }
  
      @RequestMapping(path = "/employees/{employeeId}",produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_PLAIN_VALUE})
      public @ResponseBody Employee retrievebyId(@PathVariable String employeeId) {
          return employeeService.retrieveEmployee(employeeId);
      }
  
      @PostMapping(path="/employees", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
      public ResponseEntity&lt;Void&gt; registeremployee(@RequestBody Employee newemployee) {
  
          Employee employee = employeeService.addEmployee(newemployee.getId(),newemployee.getName(), newemployee.getDescription());
  
          if (employee == null)
              return ResponseEntity.noContent().build();
  
          URI location = ServletUriComponentsBuilder.fromCurrentRequest().path(
                  "/{id}").buildAndExpand(employee.getId()).toUri();
  
          return ResponseEntity.created(location).build();
      }
  
  }
  
  </code></pre>
  
  <p>EmployeeService.java</p>
  
  <pre><code>@Component
  public class EmployeeService implements Serializable {
  
  
      static HashMap&lt;String, Employee&gt; employees = new HashMap&lt;&gt;();
  
      static {
          //Initialize Data
  
          Team team1 = new Team("t1", "Java team", "Java Dev Team");
  
          Employee Joe = new Employee("employee1", "Joe Smith","Human Resources");
  
          Employee Bob = new Employee("employee2", "Bob Jones",
                  "Developer");
  
          employees.put("employee1", Joe);
          employees.put("employee2", Bob);
      }
  
      public HashMap&lt;String, Employee&gt; retrieveAllEmployees() {
          return employees;
      }
  
      public Employee retrieveEmployee(String employeeId) {
          return employees.get(employeeId);
      }
      //private SecureRandom random = new SecureRandom();
  
      public Employee addEmployee(String id, String name, String description) {
  
          //String randomId = new BigInteger(130, random).toString(32);
          Employee employee = new Employee(id, name, description);
  
          employees.put(id, employee);
  
          return employee;
  
  
  
      }
  
      public HashMap&lt;String, Employee&gt; getEmployees(){
          return employees;
      }
  
      public void setEmployees(HashMap&lt;String, Employee&gt; employees) {
          this.employees = employees;
      }
  
  
  }
  </code></pre>
  
  <p>Employee.java</p>
  
  <pre><code>@XmlRootElement
  public class Employee implements Serializable {
  
    private String id;
  
    private String name;
  
    private String description;
    // private List&lt;Team&gt; teams;
  
    public Employee() {
  
    }
  
    public Employee(String id, String name, String description) {
      this.id = id;
      this.name = name;
      this.description = description;
      // this.teams = teams;
  
    }
  
    @XmlAttribute
    public String getId() {
      return id;
    }
  
    public void setId(String id) {
      this.id = id;
    }
  
    @XmlElement
    public String getName() {
      return name;
    }
  
    public void setName(String name) {
      this.name = name;
    }
  
    @XmlElement
    public String getDescription() {
      return description;
    }
  
    public void setDescription(String description) {
      this.description = description;
    }
  
    @Override
    public String toString() {
      return String.format("employee [id=%s, name=%s, description=%s]", id, name, description);
    }
  
  </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;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.1.6.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.springboot&lt;/groupId&gt;
      &lt;artifactId&gt;employee-model&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;employee-model&lt;/name&gt;
      &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
      &lt;properties&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;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jaxb&lt;/groupId&gt;
          &lt;artifactId&gt;jaxb-runtime&lt;/artifactId&gt;
          &lt;version&gt;2.3.2&lt;/version&gt;
          &lt;/dependency&gt;
  
              &lt;dependency&gt;
      &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
      &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
      &lt;version&gt;1.9.13&lt;/version&gt;
  &lt;/dependency&gt;
          &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
      &lt;version&gt;5.1.9.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
      &lt;version&gt;5.1.9.RELEASE&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>Log</p>
  
  <pre><code>13:13:09.818 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET http://localhost:8080/employees/
  13:13:09.831 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
  13:13:09.857 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
  13:13:09.859 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "application/json;charset=UTF-8"
  {"employee1":{"id":"employee1","name":"Joe Smith","description":"Human Resources"},"employee2":{"id":"employee2","name":"Bob Jones","description":"Developer"}}
  </code></pre>