[英]Return XML response Rest API with Spring
我正在嘗試使用 XML 響應給定的 API 呼叫。
現在它適用於 JSON,我可以發送 JSON 或 XML 並返回 JSON。
但我不能對 XML 做同樣的事情。
從現在開始,我所擁有的是:
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;
}
}
我測試了你的代碼,它對我有用。 敬請期待:
首先,我為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;
}
}
然后我必須稍微修改你的 controller(刪除Transaction
注釋,因為我的服務模擬中沒有數據源)。
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());
}
}
我還必須為Voter
創建模擬,因為您不共享它。
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 + "]";
}
}
最后是集成測試。
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());
}
}
結果可以在測試的 output 中找到,應該包含兩行。
{"login":"email","password":"secret"}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><voter><login>email</login><password>secret</password></voter>
您需要一個 XML 映射庫,例如 jackson,因此將其添加到您的 pom.xml 將解決您的問題:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
produces = { MediaType.APPLICATION_JSON_VALUE} 是我的解決方案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.