1.导入jar包(除spring之外的几个jar包)
jackson-core-asl-1.4.2.jar
jackson-mapper-asl-1.4.2.jar
xstream-1.4.4.jar
xmlpull_1_0_5.jar
2 .配置文件
先看 spring-mvc的配置文件配置,在配置文件中添加以下内容:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" p:messageConverters-ref="messageConverters"/> <util:list id="messageConverters"> <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter" p:marshaller-ref="xmlMarshaller" p:unmarshaller-ref="xmlMarshaller" > </bean> </util:list> <bean id="xmlMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> <property name="streamDriver"> <bean class="com.thoughtworks.xstream.io.xml.StaxDriver"/> </property> <property name="annotatedClasses"> <list> <value>com.wondersgroup.hs.test.model.User</value> </list> </property> </bean>注:如果测试无法成功,配置文件中如果有<mvc:annotation-driven/>,可以尝试删掉它,看能否成功。
3.Controller代码
package com.wondersgroup.hs.test.controller; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; 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.ResponseBody; import com.wondersgroup.hs.test.model.User; @Controller @RequestMapping("/hmc") public class HttpMessageConverterController { //共两种方式绑定数据 //1.使用ResponseEntity<T>/RequestEntity<T> // @RequestMapping(value="/method1") // public ResponseEntity<User> method1(HttpEntity<User> requestEntity){ // User user = requestEntity.getBody(); // user.setUserName("111"); // return new ResponseEntity<User>(user,HttpStatus.OK); // } //2.使用@ResponseBody/@RequestBody @RequestMapping(value="/method1") @ResponseBody public User method1(@RequestBody User user){ user.setUserName("111"); return user; } }
package test.com.wondersgroup.hs.test.controller; import java.io.IOException; import java.util.Collections; import org.junit.Test; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; import org.springframework.oxm.xstream.XStreamMarshaller; import org.springframework.web.client.RestTemplate; import com.thoughtworks.xstream.io.xml.StaxDriver; import com.wondersgroup.hs.test.model.User; public class HttpMessageConverterControllerTest { @Test public void testMethod1() throws IOException{ try{ RestTemplate restTemplate = buildRestTemplate(); User user = new User(); user.setUserName("tom"); user.setPassword("1234"); user.setRealName("汤姆"); HttpHeaders entityHeaders = new HttpHeaders(); //xml传输 entityHeaders.setContentType(MediaType.valueOf("application/xml;UTF-8")); entityHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML)); //json传输 //entityHeaders.setContentType(MediaType.valueOf("application/json;UTF-8")); //entityHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); HttpEntity<User> requestEntity = new HttpEntity<User>(user,entityHeaders); ResponseEntity<User> responseEntity = restTemplate.exchange( "http://localhost:8081/SpringmvcDemo/hmc/method1.html", HttpMethod.POST, requestEntity, User.class); User responseUser = responseEntity.getBody(); System.out.println(responseUser); // Assert.assertNotNull(responseUser); // Assert.assertEquals("1000", responseUser.getUserName()) }catch(Exception e){ e.printStackTrace();} } private RestTemplate buildRestTemplate(){ RestTemplate restTemplate = new RestTemplate(); XStreamMarshaller xmlMarshaller = new XStreamMarshaller(); xmlMarshaller.setStreamDriver(new StaxDriver()); xmlMarshaller.setAnnotatedClasses(new Class[]{User.class}); MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter(); xmlConverter.setMarshaller(xmlMarshaller); xmlConverter.setUnmarshaller(xmlMarshaller); restTemplate.getMessageConverters().add(xmlConverter); MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter(); restTemplate.getMessageConverters().add(jsonConverter); return restTemplate; } }
//xml传输
entityHeaders.setContentType(MediaType.valueOf("application/xml;UTF-8"));
entityHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
//json传输
//entityHeaders.setContentType(MediaType.valueOf("application/json;UTF-8"));
//entityHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));