最近用Spring MVC4.0版本发现,加了@ResponseBody注解以后,若有中文,则返回乱码。下面上解决方案:
添加POM依赖:
<!-- 这个依赖用于解决json中文乱码使用(好神奇) -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
//这里的配置必须在<mvc:annotation-driven/>之前,否则中文依然乱码
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</property>
</bean>
<mvc:annotation-driven/>
注意:
上面的配置一定要在<mvc:annotation-driven/>之前,否则依然失效!!!!
本文介绍了解决SpringMVC 4.0版本中使用@ResponseBody注解导致的中文乱码问题的方法。通过添加依赖及配置StringHttpMessageConverter来设置正确的字符集。
1660

被折叠的 条评论
为什么被折叠?



