配置方法一
1、导入第三方的fastjson包,fastjson-1.1.34.jar
2、spring-mvc.xml配置文件添加:**
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
配置方法二
1、导入第三方的jackson包,jackson-mapper-asl-1.9.7.jar和jackson-core-asl-1.9.7.jar。
2、spring-mvc.xml配置文件添加:**
<mvc:annotation-driven/> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 --> </list> </property> </bean>
配置方案三
1、需要导入 jackson-annotations-*.jar,jackson-core-.jar,jackson-databind-.jar
2、spring-mvc.xml配置文件添加:**
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html; charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html; charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean>
配置方案四
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<!-- 处理responseBody 里面日期类型 -->
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
<!-- 为null字段时不显示 -->
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
配置方案五
spring mvc 3.1之后使用RequestMappingHandlerMapping和RequestMappingHandlerAdapter
替代了原有的DefaultMethodHandlerMapping和 AnnotationMethodHandlerAdapter。
所以,3.1的返回json配置可以使用如下配置:
1. <bean
2. class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
3. p:ignoreDefaultModelOnRedirect="true" >
4. <property name="messageConverters">
5. <list>
6. <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
7. </list>
8. </property>
9. </bean>
// WEB-INF/lib 中添加
jackson-mapper-asl-1.6.4.jar
jackson-core-asl-1.6.4.jar
// Controller 中 使用:@ResponseBody 标注方法,该方法返回Java对象(支持复杂对象),
如:
@ResponseBody
1. public User getUser(User u){
2. return u;
3. {color:black}}
MappingJacksonHttpMessageConverter 会将对象转换为 JSON 输出
注意:当springMVC-servlet.xml中使用<mvc:annotation-driven />时,如果是3.1之前已经默认注入AnnotationMethodHandlerAdapter,3.1之后默认注入RequestMappingHandlerAdapter,只需加上上面提及的jar包即可!
注意:默认情况下MappingJacksonHttpMessageConverter 会设置content为application/json,在IE9下返回会出现提示下载的现象,出现这种情况可以手动指定头信息为"text/html",或者"/"(所有,不确定就设置为这个)。如果是手动注入RequestMappingHandlerAdapter 可以这样设置
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >
<property name="ignoreDefaultModelOnRedirect" value="true" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"
p:supportedMediaTypes="/"/>
</list>
</property>
</bean>
配置方案六
如果是采用mvc:annotation-driven标签 可以这样注入信息
1. <mvc:annotation-driven>
2. <mvc:message-converters register-defaults="true">
3. <bean
4. class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"
5. p:supportedMediaTypes="/" />
6. </mvc:message-converters>
7. </mvc:annotation-driven>
注入的message-converters优先级高于默认注入的json转换器。
配置方案七
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>