Spring MVC的自动转换功能 HttpMessageConverter

Spring MVC的自动转换功能 HttpMessageConverter
默认起用的MVC注解功能

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
起用JSON转换功能
xml

 1     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
 2     <bean
 3         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
 4         <property name="messageConverters">
 5             <util:list id="beanList">
 6                 <ref bean="stringHttpMessageConverter" />
 7                 <ref bean="jsonHttpMessageConverter" />
 8                 <ref bean="marshallingHttpMessageConverter" />
 9             </util:list>
10         </property>
11     </bean>
12 
13     <bean id="stringHttpMessageConverter"
14         class="org.springframework.http.converter.StringHttpMessageConverter" />
15     <bean id="jsonHttpMessageConverter"
16         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
17     <bean id="marshallingHttpMessageConverter"
18         class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
19         <property name="marshaller" ref="castorMarshaller" />
20         <property name="unmarshaller" ref="castorMarshaller" />
21     </bean>
22 
23     <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" />
24 
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;
    }

需要依赖JSON对象的处理JAR包
jackson-core-lgpl.jar
jackson-mapper-lgpl.jar
下载地址:
http://jackson.codehaus.org/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值