一、设置Tomcat过滤器
在web.xml中添加过滤器,注意要在所有过滤器之前。
<!-- spring的过滤器 解决请求中的字符编码问题 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二、设置Spring编码
<!-- 从请求和响应读取/编写字符串 -->
<bean id="stringConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
</list>
</property>
</bean>
三、设置注解
设置produces属性为响应的响应类型
@RequestMapping(value = "/testajax",produces = "text/plain;charset=UTF-8")