上一篇分析了Spring中是如何通过使用@ResponseBody注解,实现自动转换Java对象为XML,JSON数据到Reponse中。
接下来看看如果request中包含了XML或者JSON数据,反过来,如何来通过@RequestBody注解来自动转换为Java对象。
配合@RequestBody注解,以及HTTP Request Header中的Content-Type属性,HTTP Request Body中包含的XML或者JSON数据可以自动被转换成对应的Java对象。
1)首先在controller中添加handlermapping。
/**
* 根据request header中的Content-Type自动转换XML/JSON->EmployeeX对象
* 根据request header中的Accept自动选择返回XML or JSON
*/
@RequestMapping(method=RequestMethod.POST)
@ResponseBody
public EmployeeServiceResponse createEmployee(@RequestBody EmployeeX employee) {
logger.debug("creating a employee:[{}]", employee);
return new EmployeeServiceResponse(0, "");
}
package learning.webapp.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class EmployeeServiceResponse {
private Integer errcode;
private String errmsg;
public EmployeeServiceResponse() {
}
public EmployeeServiceResponse(Integer errcode, String errmsg) {
this.errcode = errcode;
this.errmsg = errmsg;
}
public Integer getErrcode() {
return errcode;
}
public void setErrcode(Integer errcode) {
this.errcode = errcode;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
}
@Test
public void testJsonRequestResponse() throws IOException, URISyntaxException {
String url = "http://localhost:8080/employees";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/json");
requestHeaders.set("Content-Type", "application/json");
String jsonStr = "{\"name\":\"Jack\",\"salary\":16000}";
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity(jsonStr, requestHeaders);
String jsonData = restTemplate.postForObject(url, httpEntity, String.class);
System.out.println(jsonData);
}
@Test
public void testXmlRequestResponse() throws IOException, URISyntaxException {
String url = "http://localhost:8080/employees";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/xml");
requestHeaders.set("Content-Type", "application/xml");
String xmlStr = "Jack 16000 ";
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity(xmlStr, requestHeaders);
String xmlData = restTemplate.postForObject(url, httpEntity, String.class);
System.out.println(xmlData);
}
@Test
public void testXmlRequestJsonResponse() throws IOException, URISyntaxException {
String url = "http://localhost:8080/employees";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/json");
requestHeaders.set("Content-Type", "application/xml");
String xmlStr = "Jack 16000 ";
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity(xmlStr, requestHeaders);
String jsonData = restTemplate.postForObject(url, httpEntity, String.class);
System.out.println(jsonData);
}
---------------------
以下引用自参考资料1:
如果不想使用
为何会覆盖,请参考博客:http://www.cnblogs.com/fangjian0423/p/spring-Ordered-interface.html
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
list>
property>
bean>
或者如果只想换messageConverters的话。
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.example.MyHttpMessageConverter"/>
<bean class="org.example.MyOtherHttpMessageConverter"/>
mvc:message-converters>
mvc:annotation-driven>
[参考资料]
1)http://www.cnblogs.com/fangjian0423/p/springMVC-xml-json-convert.html
2)http://my.oschina.net/lichhao/blog/172562