SpringMVC 使用@ResponseBody返回json 中文乱码与返回实体类报错
有时候我们发现接收的是中文,返回却是个?。这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1
/**
* Implementation of {@link HttpMessageConverter} that can read and write strings.
*
* <p>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.
*
* @author Arjen Poutsma
* @since 3.0
*/
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
既然找到问题了,那就必须想办法改过来,不同版本的Spring好像方法还不一样,网上不少说的都是Spring3.*的,现在Spring4早都出来了
更改方式可以参考
http://stackoverflow.com/questions/3616359/who-sets-response-content-type-in-spring-mvc-responsebody
http://www.cnblogs.com/chenying99/archive/2012/04/17/2453017.html
我现在用的Spring4.2.5,上面说的几个方法都试了,最后发现只有这两个可以
方法一,使用(produces = "application/json; charset=utf-8"):
@RequestMapping(value="/getUsersByPage",produces = "application/json; charset=utf-8")
// @RequestMapping("/getUsersByPage")
@ResponseBody
public String getUsersByPage(String page,String rows,String text,HttpServletRequest request,HttpServletResponse response){
方法二,在spring-mvc.xml中添加:(推荐这种)
<!-- 设定消息转换的编码为utf-8防止controller返回中文乱码 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
以上两种方式经过验证都没有问题。注意,这种方法一定要将上面配置写在 <mvc:annotation-driven/> 前面,否则会不起作用。
注意:上面的配置是不能将实体类以JSON形式放回的。需要:
1.原因:这是因为springmvc默认是没有对象转换成json的转换器的,需要手动添加jackson依赖。
2.解决步骤:
手动添加jackson依赖到pom.xml文件中
<properties>
<jackson.version>2.5.4</jackson.version>
</properties> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
如果还是没有解决,则进行以下步骤
在springmvc配置文件中进行如下配置
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
如果出现中文乱码,可以在构造函数传入参数:
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
或者:
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
附两个完整的配置(二选一即可):(有了这个不用配置方法二中的配置) 注意:下面的配置需要放在<context:component-scan base-package="cn.xm.jwxt.controller" />扫描包的配置前面
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean" >
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
或者:
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean" >
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
SpringMVC 使用@ResponseBody返回json 中文乱码与返回实体类报错的更多相关文章
- 解决springmvc使用ResponseBody注解返回json中文乱码问题
spring版本:4.2.5.RELEASE 查看“org.springframework.http.converter.StringHttpMessageConverter”源码,中有一段说明: B ...
- SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.
SpringMVC的@ResponseBody,返回json,如果有中文显示乱码的解决办法. 在SpringMVC的配置文件中 <bean class="org.springframe ...
- SpringMVC 使用@ResponseBody返回json 中文乱码
这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1 既然找到问题了,那就必须想办法改过来,不同版本的Spring好像方法还不一样,网上不少说的都是Spring3. ...
- SSM框架:解决后台传数据到前台中文乱码问题,使用@ResponseBody返回json 中文乱码
解决方法一:@RequestMapping(value="/getphone",produces = "text/plain;charset=utf-8") / ...
- 解决springmvc返回json中文乱码
在pringmvc中通过设置@ResponseBody返回json乱码问题,这个问题上网找了很久,发现答案真是人云亦云,奉上我的解决方案: 解决方案一:需要导入 jackson-core-asl-1. ...
- Post返回json中文乱码
来源:http://blog.csdn.net/xiaoxuonl/article/details/54315612 服务器返回的是utf-8,jsp页面上也是utf-8,数据库也是utf-8怎么就是 ...
- SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理
SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理... @RequestMapping(value = "/t ...
- 解决@ResponseBody注解返回的json中文乱码问题
1. 简介 主要解决@ResponseBody注解返回的json中文乱码问题. 2.解决方案 2.1mvc加上注解(推荐此方法) 在mvc配置文件中假如下面配置(写在 <mvc:annotati ...
- springMvc解决json中文乱码
springMvc解决json中文乱码 springMvc解决json中文乱码,springMvc中文乱码,spring中文乱码 >>>>>>>>> ...
随机推荐
- codeforces 793B. Igor and his way to work
B. Igor and his way to work time limit per test 3 seconds memory limit per test 256 megabytes input ...
- Nginx多进程高并发、低时延、高可靠机制在缓存(redis、memcache)twemproxy代理中的应用
1. 开发背景 现有开源缓存代理中间件有twemproxy.codis等,其中twemproxy为单进程单线程模型,只支持memcache单机版和redis单机版,都不支持集群版功能. 由于twemp ...
- Shell基础知识(六)
shell中有很多内建命令,如何区分内建命令与外部文件,使用type command即可看到命令类型. >> type cd # input << cd is a Shell ...
- codeforces 793B - Igor and his way to work(dfs、bfs)
题目链接:http://codeforces.com/problemset/problem/793/B 题目大意:告诉你起点和终点,要求你在只能转弯两次的情况下能不能到达终点.能就输出“YES”,不能 ...
- 【模板】倍增+Floyd
题目大意:给定一个 N 个顶点的邻接矩阵.起点顶点.终点顶点,求至少经过 K 条边(边可以重复)从起点到终点的最短路长度,若不能到达,输出 -1. 题解:至少经过 K 条边和恰好经过 K 条边的初始条 ...
- Pycharm激活、配置以及快捷方式 | 图解
访问flyai.club,一键创建你的人工智能项目 来源 | Python (python6359) Pycharm可以去官网下载 Pycharm的安装激活 jar包的目的就是让截获截止时间并骗过py ...
- c输出格式
#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { //取整 printf( ...
- Skype 使用小技巧
原文发布在个人独立博客上,链接:http://maxpeng.vipcloud.me/2016/09/01/skype-tricks-tips/ Skype 作为一个即时聊天工具,相信大家都不陌生 ...
- Ansible playbook 批量修改服务器密码 先普通后root用户
fsckzy Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...
- C# TextBox猜想输入和历史记录输入
什么是猜想输入 当我们在使用查询应用时,输入某个关键字后文本框下面就会自动弹出跟此关键字相附的词组供我们选择,为我们节省了输入的时间,这就是猜想输入,例如:百度,谷歌,等等,如下图: 如上图所示,使用 ...