title: Return XML response Rest API with Spring
body: <p>I am trying to response with XML to a given call to the API.</p>
  
  <p>Right now it works with JSON, I can send JSON or XML and return JSON.</p>
  
  <p>But I cannot do the same with XML.</p>
  
  <p>From now what I have is this:</p>
  
  <p>RestVoterController class:</p>
  
  <pre><code>@RequestMapping("/rest")
  @RestController
  public class RESTVoterController {
  
  @Autowired
  private VoterService voterService;
  
  @RequestMapping(value = {"/user.json","/user"},
          method = RequestMethod.POST,
          consumes = {"application/json","application/xml"},
          produces = {"application/json"})
  @Transactional(readOnly = true)
  public Voter getVoterInfoJSON(@RequestBody VoterRequestGet voterRequestGet) {
      return this.voterService.findByEmailAndPassword(voterRequestGet.getLogin(), voterRequestGet.getPassword());
  }
  
  @RequestMapping(value = "/user.xml",
          method = RequestMethod.POST,
          consumes = {"application/xml","application/json"},
          produces = "application/xml")
  @Transactional(readOnly = true)
  public Voter getVoterInfoXML(@RequestBody VoterRequestGet voterRequestGet) {
      return this.voterService.findByEmailAndPassword(voterRequestGet.getLogin(), voterRequestGet.getPassword());
  }
  
  @RequestMapping(value = "/changepassword",
          method = RequestMethod.POST,
          headers = "Accept=application/json",
          produces = "application/json")
  @Transactional(readOnly = true)
  public void changePassword(@RequestBody VoterRequestChangePassword voterRequestChangePassword) {
      this.voterService.changePassword(voterRequestChangePassword.getLogin(), voterRequestChangePassword.getOldPassword(), voterRequestChangePassword.getNewPassword());
  }
  }
  </code></pre>
  
  <p>VoterRequestGet class:</p>
  
  <pre><code>@XmlRootElement(name = "user")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class VoterRequestGet {
  
  @XmlElement
  private String login;
  @XmlElement
  private String password;
  
  public VoterRequestGet()
  {
  }
  
  public String getLogin() {
      return login;
  }
  
  public void setLogin(String login) {
      this.login = login;
  }
  
  public String getPassword() {
      return password;
  }
  
  public void setPassword(String password) {
      this.password = password;
  }
  
  }
  </code></pre>