上一篇分析了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->UserDTOX對象
* 根據request header中的Accept自動選擇傳回XML or JSON
*/
@ResponseBody
@RequestMapping(value="/createUser", method = RequestMethod.POST)
public UserDTOX createUser(@RequestBody UserDTOX userDTOX) {
logger.debug("creating a UserDTO:[{}]", userDTOX);
return new UserDTOX("Hi " + userDTOX.getName(), userDTOX.getAge() + 1);
}
2).運作以下兩個測試函數
@Test
public void testJsonRequestResponse() throws IOException, URISyntaxException {
String url = "http://localhost:8080/SpringMVC/createUser";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/json");
requestHeaders.set("Content-Type", "application/json");
String jsonStr = "{\"name\":\"Jack\",\"age\":16}";
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> httpEntity = new HttpEntity<String>(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/SpringMVC/createUser";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/xml");
requestHeaders.set("Content-Type", "application/xml");
String xmlStr = "<userDTOX><name>Jack</name><age>16</age></userDTOX>";
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> httpEntity = new HttpEntity<String>(xmlStr, requestHeaders);
String xmlData = restTemplate.postForObject(url, httpEntity, String.class);
System.out.println(xmlData);
}
3).我們甚至可以任意組合請求和應答中的XML/JSON。比如Request=XML,Response=JSON
@Test
public void testXmlRequestJsonResponse() throws IOException, URISyntaxException {
String url = "http://localhost:8080/SpringMVC/createUser";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/json");
requestHeaders.set("Content-Type", "application/xml");
String xmlStr = "<userDTOX><name>Jack</name><age>16</age></userDTOX>";
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> httpEntity = new HttpEntity<String>(xmlStr, requestHeaders);
String jsonData = restTemplate.postForObject(url, httpEntity, String.class);
System.out.println(jsonData);
}
類似上一篇中的分析,最關鍵的處理在AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters()。它會根據request header中的Content-Type屬性來選擇合适的message converter。
另外,如果不想使用<mvc:annotation-driven/>中預設的RequestMappingHandlerAdapter的話,我們可以在重新定義這個bean,spring會覆寫掉預設的RequestMappingHandlerAdapter。
<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>
文章來源:http://blog.csdn.net/fw0124/article/details/48312317