首页 > 编程语言 > 详细

解决Spring MVC @ResponseBody返回html中中文字符串乱码问题

时间:2016-10-12 11:22:56      阅读:244      评论:0      收藏:0      [点我收藏+]

最近有个应用,通过responsebody返回完整的html页面时出现乱码是异常的问题,因为是通过responsebody返回,所以一开始设置了text/plain的字符集,如下:

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
   <bean class="org.springframework.http.converter.StringHttpMessageConverter">
     <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
   </bean>
  </mvc:message-converters>
</mvc:annotation-driven>

发现还是没解决。

于是看debug日志,发现spring StringHttpMessageConverter根据内容识别出是html,根据text/html进行处理,将其改成了html/plain,问题解决,如下:

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
   <bean class="org.springframework.http.converter.StringHttpMessageConverter">
     <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
   </bean>
  </mvc:message-converters>
</mvc:annotation-driven>

解决Spring MVC @ResponseBody返回html中中文字符串乱码问题

原文:http://www.cnblogs.com/zhjh256/p/5951780.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!