spingmvc3 HttpMessageConverter返回json

本文介绍如何在SpringMVC中配置JSON数据处理,包括添加依赖、配置Bean及使用注解来实现对象到JSON的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

默认起用的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.     }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值