上一篇分析了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