解决springmvc使用ResponseBody注解返回json中文乱码问题
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中文乱码问题的更多相关文章
- SpringMVC 利用@ResponseBody注解返回Json时,出现406 not acceptable 错误的解决方法。
1 在RequestMapping中加入produces属性如: @RequestMap(value="/path",produces="application/json ...
- 【原】:关于使用springmvc的responseBody注解返回json的一些总结
配置不正确可能会出现406错误 1:首先需要导入三个jar包: 2:需要在springmvc的配置文件文件中添加转换器并开启注解驱动: 3:controller:这里返回object也是可以的; 4: ...
- [转]SpringMVC使用@ResponseBody时返回json的日期格式、@DatetimeFormat使用注意
一.SpringMVC使用@ResponseBody时返回json的日期格式 前提了解: @ResponseBody 返回json字符串的核心类是org.springframework.http.co ...
- SpringMVC使用@ResponseBody时返回json的日期格式、@DatetimeFormat使用注意
一.SpringMVC使用@ResponseBody时返回json的日期格式 前提了解: @ResponseBody 返回json字符串的核心类是org.springframework.http.co ...
- 解决spring-mvc @responseBody注解返回json 乱码问题
在使用spring-mvc的mvc的时候既享受它带来的便捷,又头痛它的一些问题,比如经典的中文乱码问题.现在是用json作为客户端和服务端 的数据交换格式貌似很流行,但是在springmvc中有时候会 ...
- SpringMVC使用@ResponseBody注解返回中文字符串乱码的问题
先说一下我的经历,以及解决问题的而过程. 在使用SpringMVC的时候,最开始的时候在配置文件中使用<mvc:annotation-driven />去自动注册DefaultAnnota ...
- spring mvc 返回乱码SpringMVC使用@ResponseBody注解返回中文字符串乱码的问题
原文地址:https://www.cnblogs.com/fzj16888/p/5923232.html 先说一下我的经历,以及解决问题的而过程. 在使用SpringMVC的时候,最开始的时候在配置文 ...
- SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.
SpringMVC的@ResponseBody,返回json,如果有中文显示乱码的解决办法. 在SpringMVC的配置文件中 <bean class="org.springframe ...
- SSM框架:解决后台传数据到前台中文乱码问题,使用@ResponseBody返回json 中文乱码
解决方法一:@RequestMapping(value="/getphone",produces = "text/plain;charset=utf-8") / ...
随机推荐
- debian linux 下设置开机自启动
懒得写了,一张图简单明了.其实很简单,一开始没有加上nohup有点问题.现在问题已解决.
- Android 消息分发机制
Android 中针对耗时的操作,放在主线程操作,轻者会造成 UI 卡顿,重则会直接无响应,造成 Force Close.同时在 Android 3.0 以后,禁止在主线程进行网络请求. 针对耗时或者 ...
- google云使用记录
gcloud工具在win下安装十分不便,而且由于众所周知的原因,无法连接,我的解决办法就是安装另外一个vps(centos7系统),然后在那里安装gcloud工具,对google进行远程访问. 1.利 ...
- Unity创建一个简易的弹簧(弹动)效果
参考文章:http://www.cnblogs.com/SkyD/archive/2008/09/05/1284778.html 主要依据胡克公式F=-k·x.这里k是倔度系数,可以理解为k值越大弹性 ...
- 随便浏览感觉简单易用的Orm
https://www.cnblogs.com/babietongtianta/p/4365195.html CYQ ITDOS CHOLE.ORM
- hibernate中的saveOrUpdate()报错
最近使用hibernate保存数据的时候,没有使用id自增主键,而是使用了一种调用seq以后手动赋值的方式生成主键,然后使用saveorupdate()报错.上网查看终于知道了问题所在,记录一下: 在 ...
- Angularjs学习笔记5_scope和$rootScope
$rootScope $rootScope 是最顶级的scope,它对应着含有 ng-app 指令属性的那个DOM元素. app.run(function($rootScope) { ...
- HTML5学习笔记(二十三):DOM应用之动态加载脚本
同步加载和执行JS的情况 在HTML页面的</body>表情之前添加的所有<script>标签,无论是直接嵌入JS代码还是引入外部js代码都是同步执行的,这里的同步执行指的是在 ...
- 每日英语:Got a Case of the Mondays? Blame the Sunday Blues
Welcome to Monday morning at the office. Did you have trouble sleeping last night? Was your stomach ...
- canvas的图片绘制案例
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...