Return XML response Rest API with Spring
17,594
I tested your code and it works for me. Please look:
First I create mock for voterService
.
package com.example;
import org.springframework.stereotype.Service;
@Service
public class VoterService {
public Voter findByEmailAndPassword(String login, String password) {
Voter voter = new Voter();
voter.setLogin(login);
voter.setPassword(password);
return voter;
}
}
Then I have to slightly modify your controller (remove Transaction
annotation because I have no data source in my service mock).
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@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"})
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")
public Voter getVoterInfoXML(@RequestBody VoterRequestGet voterRequestGet) {
return this.voterService.findByEmailAndPassword(voterRequestGet.getLogin(), voterRequestGet.getPassword());
}
}
Also I have to create mock for Voter
because you don't share it.
package com.example;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "voter")
@XmlAccessorType(XmlAccessType.FIELD)
public class Voter {
@XmlElement
private String login;
@XmlElement
private String password;
public Voter() {
}
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;
}
@Override
public String toString() {
return "Voter [login=" + login + ", password=" + password + "]";
}
}
And finally integration test.
package com.example;
import javax.annotation.Resource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class)
@WebIntegrationTest("server.port:0")
public class RESTVoterControllerTest {
private ServerProperties serverProperties;
private RestTemplate restTemplate = new RestTemplate();
@Resource
public void setServerProperties(ServerProperties serverProperties) {
this.serverProperties = serverProperties;
}
@Value("${local.server.port}")
private int serverPort;
private String serverUri;
@Before
public void setUp() throws Exception {
String contextPath = serverProperties.getContextPath();
serverUri = "http://localhost:" + serverPort + (contextPath == null ? "/" : contextPath);
}
@After
public void tearDown() throws Exception {
}
@Test
public void testCreate() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
VoterRequestGet voterRequest = new VoterRequestGet();
voterRequest.setLogin("email");
voterRequest.setPassword("secret");
HttpEntity<VoterRequestGet> request = new HttpEntity<>(voterRequest, headers);
System.out.println(restTemplate.postForEntity(
serverUri + "/rest/user.json", request, String.class).getBody());
System.out.println(restTemplate.postForEntity(
serverUri + "/rest/user.xml", request, String.class).getBody());
}
}
The result could be find in a output of the test and should contain two lines.
{"login":"email","password":"secret"}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><voter><login>email</login><password>secret</password></voter>
Author by
Roberto Fernandez Diaz
Updated on June 04, 2022Comments
-
Roberto Fernandez Diaz almost 2 years
I am trying to response with XML to a given call to the API.
Right now it works with JSON, I can send JSON or XML and return JSON.
But I cannot do the same with XML.
From now what I have is this:
RestVoterController class:
@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()); } }
VoterRequestGet class:
@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; } }