解决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") / ...
随机推荐
- 尼康G镜头与D镜头的差别
尼康系统实现自动对焦必须:机身和镜头,两者至少其一拥有马达. 尼康现在新出的G头,几乎都带马达.但在胶片时代的G头,几乎都不带马达. G只表示:没有手动光圈环. D只表示:镜头能提供距离信息给机身. ...
- windowns 2008(apache2.2.25 x86 openssl0.98y) 升级openssl1.0.1e(为了支持小程序接口TLS1.2)
原来只知道微信小程序,服务器接口必须支持 https, 所以在搭建环境的时候没有注意, 只是在原来已有的系统框架上,稳健升级,把apache2.2.25 X86_no_openssl升级为了apach ...
- 集群扩容的常规解决:一致性hash算法
写这篇博客是因为之前面试的一个问题:如果memcached集群需要增加机器或者减少机器,那么其他机器上的数据怎么办? 最后了解到使用一致性hash算法可以解决,下面一起来学习下吧. 声明与致谢: 本文 ...
- 不错位的java .class 反编译工具推荐
我们经常会反编译看一些class文件,但是反编译出来的文件里面会有很多杂乱的东西 一直以来都是用的idea来反编译的,只要把class文件往里面一拖就行了 这么用没问题,用来看看源码什么的都OK 但是 ...
- 牛腩学ASP.NET CORE做博客(视频)
牛腩学习ASP.NET CORE做的项目,边学边做. 目录: 01-dotnetcore网站部署到centos7系统上(时长 2:03:16) 02-前期准备及项目搭建 (时长:0:23:35) 03 ...
- Atitti python2.7安装 numpy attilax总结
Atitti python2.7安装 numpy attilax总结 1.1. easy_install 安装模式,没有 easy_install ,先手动安装1 1.2. 安装setuptools ...
- Hive怎样加入第三方JAR
以增加elsaticsearch-hadoop-2.1.2.jar为例,讲述在Hive中增加第三方jar的几种方式. 1,在hive shell中增加 [hadoop@hadoopcluster78 ...
- Fluent UDF【1】:简介
前面基本完成了动网格专题的发布,不过还是有一些内容并没有更新进去,比如说in-cylinder.接触检测.2.5D网格重构等.不过这些都是小技巧,写起来挺麻烦,以后有时间再通过案例视频的方式讲解好了. ...
- Capterra Software Categories
https://www.capterra.com/categories this software categories is valuable.
- 【ARM】2410裸机系列-ADC数模转换
开发环境 1.硬件平台:FS2410 2.主机:Ubuntu 12.04 ADC寄存器配置 1.初始化ADC(ADCCON) 设置预分频,预分频因子,选择A/D转换通道,并选择正常模式 ...