Spring MVC的HTTP请求信息转换器HttpMessageConverter

来源:互联网 发布:黄渤的唱功 知乎 编辑:程序博客网 时间:2024/05/18 03:46

当控制器上添加@ResponseBody时,返回的java对象默认转成Json字符串输出。当然我们也可以自定义输出的数据类型如:XML等等。此时我们需要配置 RequestMappingHandlerMapping和RequestMappingHandlerAdapter

前景:
要使用@Controller @ResponseBody时要在 Spsring配置文件中添加 其实这个做了很多事。
在讲解这个配置之前,我们先了解下Spring的消息转换机制。@ResponseBody这个注解就是使用消息转换机制,最终通过json的转换器转换成json数据的。
HttpMessageConverter接口就是Spring提供的http消息转换接口。
HttpMessageConverter

更好资料:SpringMVC关于json、xml自动转换的原理研究
Spring MVC之@RequestBody, @ResponseBody 详解
Spring MVC 解读——
注解式控制器运行流程及处理器定义 第六章 注解式控制器详解

  • 媒体类型
    spring 3.X
    StringHttpMessageConverter: that can read and write Strings from the HTTP request and response.

    FormHttpMessageConverter:that can read and write form data from the HTTP request and response.ByteArrayMessageConverter:that can read and write byte arrays from the HTTP request and response.MarshallingHttpMessageConverter:XML的转换需要使用Spring的 Marshaller 和 Unmarshaller.MappingJacksonHttpMessageConverter:JSON的转换.SourceHttpMessageConverter:能够读/写来自HTTP的请求与响应的javax.xml.transform.Source ,支持DOMSource, SAXSource, 和 StreamSource 的XML格式

BufferedImageHttpMessageConverter:that can read and write java.awt.image.BufferedImage from the HTTP request and response

配置:

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->    <bean        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">        <property name="messageConverters">            <util:list id="beanList">                <ref bean="stringHttpMessageConverter" />                <ref bean="jsonHttpMessageConverter" />                <ref bean="marshallingHttpMessageConverter" />            </util:list>        </property>    </bean>    <bean id="stringHttpMessageConverter"        class="org.springframework.http.converter.StringHttpMessageConverter" />    <bean id="jsonHttpMessageConverter"        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />    <bean id="marshallingHttpMessageConverter"        class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">        <property name="marshaller" ref="castorMarshaller" />        <property name="unmarshaller" ref="castorMarshaller" />    </bean>    <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" />

spring 4.2.5

StringHttpMessageConverter
can read and write Strings from the HTTP request and response
支持 text/* ;Content-Type = text/plain
FormHttpMessageConverter
can read and write form data from the HTTP request and response
默认 application/x-www-form-urlencoded
ByteArrayHttpMessageConverter
can read and write form data from the HTTP request and response

MappingJackson2HttpMessageConverter
JSON的转换

BufferedImageHttpMessageConverter
can read and write java.awt.image.BufferedImage from the HTTP request and response.
This converter reads and writes the media type supported by the Java I/O API.

读写XML需要配置 MarshallingHttpMessageConverter 的 Marshaller 和 Unmarshaller 属性,它们来自org.springframework.oxm包,如下:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">    <property name="messageConverters">        <util:list id="beanList">            <ref bean="stringHttpMessageConverter"/>            <ref bean="mappingJackson2HttpMessageConverter"/>            <ref bean="marshallingHttpMessageConverter"/>        </util:list>    </property</bean><bean id="stringHttpMessageConverter"        class="org.springframework.http.converter.StringHttpMessageConverter"/><bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/><bean id="marshallingHttpMessageConverter"        class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">    <property name="marshaller" ref="castorMarshaller"/>    <property name="unmarshaller" ref="castorMarshaller"/></bean><bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>

MappingJacksonHttpMessageConverter能够将POJO对象自动转换为JSON对象

 @RequestMapping(value = "/getPojoJson" , method=RequestMethod.GET)    public @ResponseBody Pojo getPojoJson() {      Pojo pojo=new Pojo();      pojo.setA("test");      pojo.setB(1);      pojo.setD(new Date());      return pojo;    }
    <!-- ===========HTTP请求信息转换器HttpMessageConverter start=========== -->    <!-- ====使用默认====-->    <!-- 开启springMVC注解 如:@Controller;@RequestMapping-->    <!-- <mvc:annotation-driven /> -->    <!-- ====自定义====-->    <!-- Spring3.1之前的版本 注解 HandlerMapping和HandlerAdapter       <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>     -->    <!--Spring3.1之后的版本 注解 HandlerMapping和HandlerAdapter -->     <!-- 配置HandlerMapping -->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>    <!-- XML转换器 -->    <bean id="marshallingHttpMessageConverter"            class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">        <property name="marshaller" ref="castorMarshaller"/>        <property name="unmarshaller" ref="castorMarshaller"/>    </bean>    <!-- 供marshaller和unmarshaller使用 -->    <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>    <!-- 配置HandlerAdapter -->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <property name="messageConverters">            <list>                <ref bean="marshallingHttpMessageConverter"/><!--                                <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>                <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>                <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>                <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />                 <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>-->            </list>        </property>    </bean>    <!-- ===========HTTP请求信息转换器HttpMessageConverter end=========== -->
0 0
原创粉丝点击