spring responsebody 返回中文乱码

@RequestMapping(value = "getJSON", method = RequestMethod.GET,produces={"text/html;charset=UTF-8;","application/json;"} )

    @ResponseBody

    public String getJson(HttpServletRequest request,HttpServletResponse response){

         response.setCharacterEncoding("UTF-8");

        

        return "说到底";

        

    }

前台

.ajax({


       type: "GET",

       url: "getJSON.do",

       success: function(data){

           console.log(data);          

       }

   网上说加produces=“ { }”

   加了还是乱码。


大话西游666
浏览 765回答 6
6回答

一只萌萌小番薯

还得看你的web容器是什么编码啊... 踩着踩着也就过去了.

慕森王

对spring-mvc对编码适配器做编码支持,没记错是标签名为:<mvc:message-converters>的货

婷婷同学_

spring mvc 添加 Jackson 支持添加spring mvc 配置<mvc:annotation-driven>&nbsp; &nbsp; <mvc:message-converters>&nbsp; &nbsp; &nbsp; &nbsp; <bean class="org.springframework.http.converter.StringHttpMessageConverter">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <constructor-arg name="defaultCharset" value="UTF-8"/>&nbsp; &nbsp; &nbsp; &nbsp; </bean>&nbsp; &nbsp; </mvc:message-converters></mvc:annotation-driven>或者 重写 StringHttpMessageConverter/* 将 */ public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");/* 改 */ public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

LEATH

用其他的抓包工具测试你的返回结果是否是utf-8的中文。问题往往出现在源头或者最后一步。如果是在页面乱码上你的JSP页面没有设置 charset "UTF-8"。浏览器console里面乱码的话,用F12 网络 看看响应头Content-Type = text/html,Encoding之类的。这些环节没有问题,那大概就是String 环境设置的问题,前面回答很值得参考。

小唯快跑啊

工程中用springmvc返回json格式时,中文乱码了,看了一下springmvc源码发现 StringHttpMessageConverter 这个类的默认编码为ISO-8859-1(悲剧,springmvc这么大的东西怎么不用utf-8,搞不懂)下面是解决方法,springmvc 的配置文件:<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >&nbsp; &nbsp; &nbsp; &nbsp; <property name="messageConverters">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <list>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <bean class = "org.springframework.http.converter.StringHttpMessageConverter">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <property name = "supportedMediaTypes">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <list>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>text/html;charset=UTF-8</value>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </list>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </bean>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </list>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp;&nbsp;&nbsp; &nbsp; </bean>&nbsp; &nbsp; <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <property name="messageConverters">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <list>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <bean class="org.springframework.http.converter.StringHttpMessageConverter">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <property name="supportedMediaTypes">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <list>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>text/html; charset=utf-8</value>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </list>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </bean>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </list>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp;&nbsp; &nbsp; </bean>&nbsp; &nbsp; <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <property name="interceptors">&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <list>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </list>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp;&nbsp;&nbsp; &nbsp; </bean>&nbsp;&nbsp;使用这个配置要把springmvc这个<mvc:annotation-driven /> 删掉,不要用springmvc默认的初始化配置

杨__羊羊

加没加CharacterEncodingFilter?没加的话加上试试看如果不行:用的哪个版本的spirng,哪个版本的tomcat?以及:哪有楼上这些说的这么复杂。。。一片一片的xml看着就烦。。。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java