最近因为项目需要,学习了Java的sping mvc框架,在使用的过程中,遇到了请求返回中文乱码的情况,研究了框架使用手册以及查阅了网上相关资料,总结出以下两种解决办法。
解决方法1:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
解决方法2:
@RequestMapping(produces = "application/json; charset=utf-8")
如上的两种办法可以解决这个问题,第一种是在spring-web.xml里面配置一个bean来对所有的请求进行转码工作;第二种是对单个请求进行编码设置,需要在每个请求前面加上这个,比较麻烦。推荐使用第一种。