SpringMVC 返回字符串 /json 数据到前台浏览器中文乱码问题

SpringMVC 直接返回字符串时,中文乱码出现问号 (?) 的问题,下列是各种解决方案:

通过配置 spring-mvc.xml
//-- 在 annotation-driven 中添加 converter
<mvc:annotation-driven>
   <mvc:message-converters>
       <bean class="org.springframework.http.converter.StringHttpMessageConverter">
           <constructor-arg ref="utf8Charset" />
       </bean>
   </mvc:message-converters>
</mvc:annotation-driven>
<bean id="utf8Charset" class="java.nio.charset.Charset" factory-method="forName">
   <constructor-arg value="UTF-8" />
</bean>

或者

SpringMVC 返回字符串 /json 数据到前台浏览器中文乱码问题
<mvc:annotation-driven >
   <!-- 消息转换器 -->
       <mvc:message-converters register-defaults="true">
         <bean class="org.springframework.http.converter.StringHttpMessageConverter">
           <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
         </bean>
       </mvc:message-converters>
   </mvc:annotation-driven>

或者

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

或者

<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
       <bean class="cn.dubby.what.util.MyStringHttpMessageConverter"/>
</mvc:annotation-driven>
在 requestMapping 中设置下编码

即在 RequestMapping 使用(produces = “text/html; charset=utf-8”)produces 作用根据请求头中的 Accept 进行匹配,如请求头“Accept:text/html”时即可匹配。

@RequestMapping(method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")

json 的话 produces 就是(根据自己需要修改):

produces = {"application/json;charset=UTF-8"}
response 返回页面的话可直接设置编码
public static void write(HttpServletResponse response,Object o)throws Exception{
   response.setContentType("text/html;charset=utf-8");
   PrintWriter out=response.getWriter();
   out.println(o.toString());
   out.flush();
   out.close();
}



目录
  • SpringMVC 直接返回字符串时,中文乱码出现问号 (?) 的问题,下列是各种解决方案:
    • 通过配置 spring-mvc.xml
    • 在 requestMapping 中设置下编码
    • response 返回页面的话可直接设置编码
  • 目录
  • SpringMVC 直接返回字符串时,中文乱码出现问号 (?) 的问题,下列是各种解决方案:
    • 通过配置 spring-mvc.xml
    • 在 requestMapping 中设置下编码
    • response 返回页面的话可直接设置编码
  • 手机扫描二维码访问

      本文标题:《SpringMVC 返回字符串 /json 数据到前台浏览器中文乱码问题》作者:极四维博客
      原文链接:https://cway.top/post/173.html
      特别注明外均为原创,转载请注明。

      分享到微信

      扫描二维码

      可在微信查看或分享至朋友圈。

      相关文章

      发表评论:

      ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

      «    2025年5月    »
      1234
      567891011
      12131415161718
      19202122232425
      262728293031

      搜索

      控制面板

      您好,欢迎到访网站!
        查看权限

      最新留言

      文章归档

      • 订阅本站的 RSS 2.0 新闻聚合