SpringMVC 使用@ResponseBody返回json 中文乱码

这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1

我现在用的Spring4.2.5,上面说的几个方法都试了,最后发现只有这两个可以

方法一,使用(produces = "application/json; charset=utf-8")(如何整个项目都是以json格式的数据来交互的不建议使用,因为每一个都需要加编码格式比较麻烦):

 @RequestMapping(value="/getUsersByPage",produces = "application/json; charset=utf-8")
//    @RequestMapping("/getUsersByPage")
    @ResponseBody
    public String getUsersByPage(String page,String rows,String text,HttpServletRequest request,HttpServletResponse response){

方法二,在spring-mvc.xml中添加:

<!-- 处理请求返回json字符串的中文乱码问题 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

猜你喜欢

转载自blog.csdn.net/xingqibaing/article/details/82848150