Springmvc使用阿里巴巴的fastjson传输到前台中文乱码的解决方案,他娘的大家都少制造垃圾,学习过程将会多么快乐
弄了大概七八个小时吧
都他妈比的抄来抄去,一分一秒的去试错
最终参考这个问题解决了乱码的情况https://bbs.csdn.net/topics/392169549
412
需要在springmvc中添加如下配置(我都使用的utf-8)
<!-- string乱码 -->
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="utf-8" index="0"/>
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=utf-8</value>
</list>
</property>
</bean>
<!-- fastJson配置 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/json;charset=utf-8</value>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
<!-- 请求处理器 -->
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="fastJsonHttpMessageConverter"/>
<ref bean="stringHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<!--fastJsonConfig -->
<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
<!--默认编码格式 -->
<property name="charset" value="UTF-8"/>
<property name="serializerFeatures">
<list>
<value>WriteNullListAsEmpty</value>
<value>WriteDateUseDateFormat</value>
<value>PrettyFormat</value>
<value>WriteMapNullValue</value>
<value>WriteNullStringAsEmpty</value>
<value>WriteNullListAsEmpty</value>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
</bean>
<!--fastjson支持配置结束 -->
至于mvc自带的jackson的配置,自动转换的配置写不写都无所谓了

控制器这样写

如果有
produces= "application/json;charset=UTF-8"
或者这个写法
produces= {"application/json;charset=UTF-8"}
完成以上配置才能正确返回中文,特别是控制器上的那一串,一定要写,
返回数据如下


如果没有上边那句话
就成了这样的

太晚了,睡觉,明天在研究
乱码问题解决,别的就都好说了
如果不适用return ,也可以这样写
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(json);

发出来的也是这样的

1

浙公网安备 33010602011771号