SpringMVC提供了几个处理XML和JSON格式的请求/响应信息的HttpMessageConverter:
只要在Spring WEB容器中为RequestMappingHandlerAdapter装配好相应的处理XML和JSON格式的请求/响应消息的HttpMessageConverter,并在交互中通过请求的Accept指定MIME类型,SpringMVC就可以使用服务器端的处理方法和客户端透明的通过XML和JSON格式的消息进行通信。
一、在WEB上下文中装配处理XML和JSON格式的请求/响应消息的HttpMessageConverter。
1.1、需要用到的jar包
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
1.2、web上下文中的配置
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
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.support.AllEncompassingFormHttpMessageConverter"/>
<!--装配解析XML和JSON的HttpMessageConverter-->
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"
p:marshaller-ref="xmlMarshaller"
p:unmarshaller-ref="xmlMarshaller"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</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.smart.bean.User</value>
</list>
</property>
</bean>
(待定)