SpringMVC使用注解@ResponseBody返回json以及中文乱码问题解决
在这里我不得支持一下SpringMVC是一个不错的框架,用了springmvc这么长时间 ,我犯了个错误,说来惭愧.
在项目中编码格式是UTF-8 ,在使用Ajax 请求, 在SpringMVC中返回Json 数据,我用到了@ResponseBody 注解 ,将表示该方法的返回结果直接写入HTTP response body中。
然后再从数据库中取出的中文通过Spring的response返回结果中文乱码.
方法一
原因:response在响应的时候 ,生成的response中"Content-Type"的值不正确。(默认的是ISO-8859-1)
然后Spring使用AnnotationMethodHandlerAdapter来处理@ResponseBody,该类再使用一些HttpMessageConverter来具体处理信息。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<context:component-scan base-package="com.zdkj.limp.controller"></context:component-scan>
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"></property>
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
参考配置2:
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" index="0"></constructor-arg>
</bean>
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
参考配置3:(如果用的是Jackson框架)
----
基于spring3.2以后的配置文件。编码配置需要放在<mvc:annotation-driven/>之前,否则无效。
在SpringMVC 的Controller中使用@ResponseBody注解向客户端返回数据时,如果没有特殊设置则中文将显示为乱码,此时需要在Spring-MVC.xml配置文件中加入如下代码:
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射,解决@ResponseBody乱码问题, 需要在annotation-
driven之前,否则乱码问题同样无法解决 -->
<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>
</list>
</property>
</bean>
</list>
</property>
</bean>
注意以上配置需要放到<mvc:annotation-driven />之前,否则无效。
=====
如果使用的是JSON-lib框架,则可以使用如下配置:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<!-- 防止返回json乱码 -->
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text" />
<constructor-arg index="1" value="plain" />
<constructor-arg index="2" value="UTF-8" />
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
不断补充中...