@ResponseBody:
将内容或对象作为 HTTP 响应正文返回,使用@ResponseBody将会跳过视图处理部分,而是调用适合HttpMessageConverter,将返回值写入输出流。
引起乱码原因为Spring mvc使用的默认处理字符串编码为ISO-8859-1。Spring MVC有一系列HttpMessageConverter去处理用@ResponseBody注解的返回值,
如MappingJacksonHttpMessageConverter 用来处理json格式转换;
返回String,则使用StringHttpMessageConverter,这个convert使用的是字符集是iso-8859-1,而且是final的。
解决方法:
第一种方式:对于需要返回字符串的方法添加注解,如下:
@RequestMapping(value="/getString", produces = "application/json; charset=utf-8")
public @ResponseBody String getString()
{
return "我不是乱码!";
}
此方法只针对单个调用方法起作用。
第二种方式:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
注意:XML的dtd要写4.0的,即 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
1、修改StringHttpMessageConverter使用字符集
<!-- 使用spring mvc -->
<mvc:annotation-driven>
<!-- 解决@ResponseBody中文乱码 -->
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
注意:需要把这段放在<context:component-scan base-package="xxxxx"/>前面。
问题:这个时候的响应头,你会发现体积非常大(Accept-Charset会达到4K+),这是因为默认情况下StringHttpMessageConverter.writeInternal()会将所有可用字符集回写到响应头中。
解决:修改StringHttpMessageConverter提供的参数:writeAcceptCharset
<!-- 用于避免响应头过大 -->
<property name="writeAcceptCharset" value="false" />
参考第三种方式写法。
2、使用MappingJackson2HttpMessageConverter转换字符串
<mvc:annotation-driven>
<!-- 返回json数据,@response使用 -->
<mvc:message-converters register-defaults="true">
<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>
</mvc:message-converters>
</mvc:annotation-driven>
注意:返回字符串带双引号,即:"我不是乱码!"。之前方法返回:我不是乱码!
第三种方式
在配置文件中加入
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="writeAcceptCharset" value="false" /> <!-- 用于避免响应头过大 -->
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
注意:需要把这段放在<mvc:annotation-driven />前面
问题:只能转换String类型,其他Map,List转换异常
解决:通过添加MappingJacksonHttpMessageConverter解决这个问题
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="writeAcceptCharset" value="false" /> <!-- 用于避免响应头过大 -->
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
也可:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
String也交给MappingJackson2HttpMessageConverter转换。返回如第二种方式2:"我不是乱码!"