spingmvc3 HttpMessageConverter返回json

来源:互联网 发布:淘宝奖池怎么设置 编辑:程序博客网 时间:2024/05/29 04:33

Spring MVC的自动转换功能 HttpMessageConverter
默认起用的MVC注解功能
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

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 ,支持DOMSourceSAXSource, 和 StreamSource 的XML格式

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


1.首先加入两个jar依赖,这里以maven构建为例: 

Xml代码  
<dependency>  
    <groupId>org.codehaus.jackson</groupId>  
    <artifactId>jackson-core-asl</artifactId>  
    <version>1.8.4</version>  
</dependency>  
<dependency>  
    <groupId>org.codehaus.jackson</groupId>  
    <artifactId>jackson-mapper-asl</artifactId>  
    <version>1.8.4</version>  
</dependency>  
这是spring MVC处理json数据时,所必须的jar依赖。 

2.在spring mvc的配置文件中加入配置 
这里我们使用的是spring mvc的注解,如例: 
Xml代码  
<context:component-scan base-package="com.hundsun.twioo.business.action" />  
<bean class="org.springframework.web.servlet.mvc.annotation.  
    DefaultAnnotationHandlerMapping" />  
<bean class="org.springframework.web.servlet.mvc.annotation.  
    AnnotationMethodHandlerAdapter" >  
    <property name="messageConverters">  
        <util:list id="beanList">  
            <ref bean="mappingJacksonHttpMessageConverter"/>  
        </util:list>  
    </property>  
</bean>  
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.  
    http.converter.json.MappingJacksonHttpMessageConverter">  
    <property name="supportedMediaTypes">  
        <list>  
            <value>text/html;charset=UTF-8</value>  
        </list>  
    </property>  
</bean>  
<context:annotation-config/> 
我测试了下,把org.springframework.http.converter.json.MappingJacksonHttpMessageConverter 
换成org.springframework.http.converter.StringHttpMessageConverter也是可行的。有关HttpMessageConverter可以参考: 
http://www.chineselinuxuniversity.net/articles/46262.shtml
 

注意:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter的Bean配置中,我们加入了messageConverters属性,在该属性中我们有配置mappingJacksonHttpMessageConverter这个Bean,它就是用来处理json数据转换的。 
在mappingJacksonHttpMessageConverter的Bean配置中,我们有一个supportedMediaTypes属性,在这个属性 
中我们添加了text/html;charset=UTF-8这个值,它是为了处理返回的json数据的编码,默认是ISO-88859-1的,这里我们把它设置为UTF-8,如果有乱码的情况,我们只需要修改这里就可以了。 

Java代码  收藏代码
  1. @RequestMapping(value="test2012",method = RequestMethod.GET)  
  2.     @ResponseBody  
  3.     public Object test(){  
  4.         Admin admin = new Admin();  
  5.         admin.setAdminname("test");  
  6.         admin.setEmail("test@qq.com");  
  7.         admin.setId(1);  
  8.         admin.setPhone("12345678900");  
  9.         return admin;  
  10.     }  



0 0
原创粉丝点击