spring版本:4.2.5.RELEASE

查看“org.springframework.http.converter.StringHttpMessageConverter”源码,中有一段说明:

By default, this converter supports all media types ({@code &#;&#;&#;}),
and writes with a {@code Content-Type} of {@code text/plain}. This can be overridden
by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.

media type :(also MIME type and content type) is a two-part identifier for file formats and format contents transmitted on the Internet.
所以可以制定文件格式和编码,
“text/html;charset=UTF-8”:解决浏览器直接访问接口时中文乱码问题。
“application/json;charset=UTF-8”:解决ajax【dataType:"json"】时中文乱码问题。

在springmvc的配置文件中加入如下配置:

    <!-- 让DispatchServlet启动基于annotation的HandlerMapping -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 解决Controller返回json中文乱码问题 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<!-- <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> -->
<!-- <property name="supportedMediaTypes" value="application/json;charset=UTF-8" > -->
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-</value>
<value>application/json;charset=UTF-</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

==================== 分割线  ====================

其他还有各种中文乱码问题:

1、jsp:声明页面编码方式

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
……
</body>
</html>

2、过滤器:在web.xml中加入如下代码

  <!-- 中文编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>

3、修改tomcat的server.xml

<Connector URIEncoding="UTF-8" connectionTimeout="" port="" protocol="HTTP/1.1" redirectPort=""/>

总结:搞清楚出现乱码的原因以及每一个配置的作用是解决出现乱码的关键。

QQ技术交流群:282575808

--------------------------------------

声明: 原创文章,未经允许,禁止转载!

--------------------------------------

解决springmvc使用ResponseBody注解返回json中文乱码问题的更多相关文章

  1. SpringMVC 利用@ResponseBody注解返回Json时,出现406 not acceptable 错误的解决方法。

    1 在RequestMapping中加入produces属性如: @RequestMap(value="/path",produces="application/json ...

  2. 【原】:关于使用springmvc的responseBody注解返回json的一些总结

    配置不正确可能会出现406错误 1:首先需要导入三个jar包: 2:需要在springmvc的配置文件文件中添加转换器并开启注解驱动: 3:controller:这里返回object也是可以的; 4: ...

  3. [转]SpringMVC使用@ResponseBody时返回json的日期格式、@DatetimeFormat使用注意

    一.SpringMVC使用@ResponseBody时返回json的日期格式 前提了解: @ResponseBody 返回json字符串的核心类是org.springframework.http.co ...

  4. SpringMVC使用@ResponseBody时返回json的日期格式、@DatetimeFormat使用注意

    一.SpringMVC使用@ResponseBody时返回json的日期格式 前提了解: @ResponseBody 返回json字符串的核心类是org.springframework.http.co ...

  5. 解决spring-mvc @responseBody注解返回json 乱码问题

    在使用spring-mvc的mvc的时候既享受它带来的便捷,又头痛它的一些问题,比如经典的中文乱码问题.现在是用json作为客户端和服务端 的数据交换格式貌似很流行,但是在springmvc中有时候会 ...

  6. SpringMVC使用@ResponseBody注解返回中文字符串乱码的问题

    先说一下我的经历,以及解决问题的而过程. 在使用SpringMVC的时候,最开始的时候在配置文件中使用<mvc:annotation-driven />去自动注册DefaultAnnota ...

  7. spring mvc 返回乱码SpringMVC使用@ResponseBody注解返回中文字符串乱码的问题

    原文地址:https://www.cnblogs.com/fzj16888/p/5923232.html 先说一下我的经历,以及解决问题的而过程. 在使用SpringMVC的时候,最开始的时候在配置文 ...

  8. SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.

    SpringMVC的@ResponseBody,返回json,如果有中文显示乱码的解决办法. 在SpringMVC的配置文件中 <bean class="org.springframe ...

  9. SSM框架:解决后台传数据到前台中文乱码问题,使用@ResponseBody返回json 中文乱码

    解决方法一:@RequestMapping(value="/getphone",produces = "text/plain;charset=utf-8") / ...

随机推荐

  1. ARM:移动GPU往PC GPU效能迈进

    行动装置的热潮持续不退,各大手机制造商除了想尽办法推出外型酷炫的行动装置设备来吸引消费者的目光之外,更在行动应用处理器玩起多核心的「核」战争,无非是希望能够带给消费者更优异的效能新体验.然而,随着消费 ...

  2. Flume 相关

    在CentOS 7上安装配置Flume https://mos.meituan.com/library/41/how-to-install-flume-on-centos7/ Flume NG 学习笔 ...

  3. rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted

    今天在同步数据的时候提示rsync: failed to set times on “xxxx”: Operation not permitted,一般来说要不是服务器时间不对或者权限没有设置好,下面 ...

  4. 两条命令在Linux主机之间建立信任关系

    ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa //生成当前用户密钥 ssh-copy-id -i /root/.ssh/id_rsa.pub r ...

  5. opencv之haar特征+AdaBoos分类器算法流程(三)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  6. Android-Cannot merge new index 66195 into a non-jumbo instruction的解决办法

    转载请注明来源:http://blog.csdn.net/goldenfish1919/article/details/33729679 用eclispe打包的时候报错: [2014-06-23 13 ...

  7. iOS缓存-内存缓存

    为了减少与服务器的连接沟通次数,提高应用程序的执行速度,使用了iOS的缓存机制 #import "YoungViewController.h" @interface YoungVi ...

  8. 读取JPG图片的Exif属性(一) - Exif信息简介

    https://blog.csdn.net/fioletfly/article/details/53605959 https://blog.csdn.net/a_big_pig/article/det ...

  9. MyBatis传入集合 list 数组 map参数的写法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...

  10. android studio - 右键没有“New C++ Class 选项” 或 “Cannot find declaration to go to”的问题

    今天在android studio的cpp文件夹上右键,发现竟然没有了New C++ Class“的选项,如下图所示: 上图是正常情况. 下图是非正常情况. 解决方案 检查”File“-"S ...