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消息转换接口。
更好资料: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=========== -->
- Spring MVC的HTTP请求信息转换器HttpMessageConverter
- Spring MVC框架下有关HTTP请求信息转换器HttpMessageConverter
- spring-mvc 请求body 的处理方式 HttpMessageConverter
- Spring MVC的自动转换功能 HttpMessageConverter
- Spring MVC的自动转换功能 HttpMessageConverter
- Spring MVC的自动转换功能 HttpMessageConverter
- Spring MVC的自动转换功能 HttpMessageConverter
- Spring MVC的自动转换功能 HttpMessageConverter
- spring mvc HttpMessageConverter使用
- spring mvc 环境Fastjson转换器jsonp请求
- springmvc HttpMessageConverter(http消息转换器)
- Spring MVC 的http请求处理过程
- spring MVC 转换器的使用
- Spring MVC4.1-Spring MVC的配置(含自定义HttpMessageConverter)
- spring mvc 环境Jackson转换器jsonp请求=方式1
- spring mvc 环境Jackson转换器jsonp请求=方式2
- spring mvc 拦截器打印详细的请求参数信息
- Spring MVC 学习4:与HTTP请求相关的映射
- 探索Java应用程序的国际化支持
- J2EE学习(Hibernate的工作原理)
- iOS开发,图片太多占内存太大如何解决
- Hadoop 文件块大小设置
- Longest Palindromic Substring
- Spring MVC的HTTP请求信息转换器HttpMessageConverter
- iOS自定义控件-动态标签展示
- jquery数字滚动
- COM简介
- 交换排序
- Docker应用的监控
- Java数组取交集
- android通过电源管理保持屏幕常亮
- JS判断数据类型