有时候我们发现接收的是中文,返回却是个?。这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1

  1. /**
  2. * Implementation of {@link HttpMessageConverter} that can read and write strings.
  3. *
  4. * <p>By default, this converter supports all media types ({@code */*}),
  5. * and writes with a {@code Content-Type} of {@code text/plain}. This can be overridden
  6. * by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
  7. *
  8. * @author Arjen Poutsma
  9. * @since 3.0
  10. */
  11. public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
  12.  
  13. 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"):

  1. @RequestMapping(value="/getUsersByPage",produces = "application/json; charset=utf-8")
  2. // @RequestMapping("/getUsersByPage")
  3. @ResponseBody
  4. public String getUsersByPage(String page,String rows,String text,HttpServletRequest request,HttpServletResponse response){

方法二,在spring-mvc.xml中添加:(推荐这种)

  1. <!-- 设定消息转换的编码为utf-8防止controller返回中文乱码 -->
  2. <bean
  3. class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  4. <property name="messageConverters">
  5. <list>
  6. <bean
  7. class="org.springframework.http.converter.StringHttpMessageConverter">
  8. <property name="supportedMediaTypes">
  9. <list>
  10. <value>text/html;charset=UTF-8</value>
  11. </list>
  12. </property>
  13. </bean>
  14. </list>
  15. </property>
  16. </bean>

以上两种方式经过验证都没有问题。注意,这种方法一定要将上面配置写在 <mvc:annotation-driven/> 前面,否则会不起作用。

注意:上面的配置是不能将实体类以JSON形式放回的。需要:

 1.原因:这是因为springmvc默认是没有对象转换成json的转换器的,需要手动添加jackson依赖。

  2.解决步骤:

    手动添加jackson依赖到pom.xml文件中

  1. <properties>
  2. <jackson.version>2.5.4</jackson.version>
  3. </properties>
  4.  
  5. <dependency>
  6. <groupId>com.fasterxml.jackson.core</groupId>
  7. <artifactId>jackson-core</artifactId>
  8. <version>${jackson.version}</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>com.fasterxml.jackson.core</groupId>
  12. <artifactId>jackson-databind</artifactId>
  13. <version>${jackson.version}</version>
  14. </dependency>

  如果还是没有解决,则进行以下步骤

  在springmvc配置文件中进行如下配置

  1. <mvc:annotation-driven>
  2. <mvc:message-converters>
  3. <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
  4. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
  5. </mvc:message-converters>
  6. </mvc:annotation-driven>

如果出现中文乱码,可以在构造函数传入参数:

  1. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  2. <constructor-arg value="UTF-8" />
  3. </bean>

或者:

  1. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  2. <property name="supportedMediaTypes">
  3. <list>
  4. <value>text/html;charset=UTF-8</value>
  5. </list>
  6. </property>
  7. </bean>

附两个完整的配置(二选一即可):(有了这个不用配置方法二中的配置)  注意:下面的配置需要放在<context:component-scan base-package="cn.xm.jwxt.controller" />扫描包的配置前面

  1. <mvc:annotation-driven conversion-service="conversionServiceFactoryBean" >
  2. <mvc:message-converters>
  3. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  4. <property name="supportedMediaTypes">
  5. <list>
  6. <value>text/html;charset=UTF-8</value>
  7. </list>
  8. </property>
  9. </bean>
  10. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  11. <property name="supportedMediaTypes">
  12. <list>
  13. <value>text/html;charset=UTF-8</value>
  14. </list>
  15. </property>
  16. </bean>
  17. </mvc:message-converters>
  18. </mvc:annotation-driven>

或者:

  1. <mvc:annotation-driven conversion-service="conversionServiceFactoryBean" >
  2. <mvc:message-converters>
  3. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  4. <constructor-arg value="UTF-8" />
  5. </bean>
  6. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  7. <property name="supportedMediaTypes">
  8. <list>
  9. <value>text/html;charset=UTF-8</value>
  10. </list>
  11. </property>
  12. </bean>
  13. </mvc:message-converters>
  14. </mvc:annotation-driven>

SpringMVC 使用@ResponseBody返回json 中文乱码与返回实体类报错的更多相关文章

  1. 解决springmvc使用ResponseBody注解返回json中文乱码问题

    spring版本:4.2.5.RELEASE 查看“org.springframework.http.converter.StringHttpMessageConverter”源码,中有一段说明: B ...

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

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

  3. SpringMVC 使用@ResponseBody返回json 中文乱码

    这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1 既然找到问题了,那就必须想办法改过来,不同版本的Spring好像方法还不一样,网上不少说的都是Spring3. ...

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

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

  5. 解决springmvc返回json中文乱码

    在pringmvc中通过设置@ResponseBody返回json乱码问题,这个问题上网找了很久,发现答案真是人云亦云,奉上我的解决方案: 解决方案一:需要导入 jackson-core-asl-1. ...

  6. Post返回json中文乱码

    来源:http://blog.csdn.net/xiaoxuonl/article/details/54315612 服务器返回的是utf-8,jsp页面上也是utf-8,数据库也是utf-8怎么就是 ...

  7. SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理

    SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理... @RequestMapping(value = "/t ...

  8. 解决@ResponseBody注解返回的json中文乱码问题

    1. 简介 主要解决@ResponseBody注解返回的json中文乱码问题. 2.解决方案 2.1mvc加上注解(推荐此方法) 在mvc配置文件中假如下面配置(写在 <mvc:annotati ...

  9. springMvc解决json中文乱码

    springMvc解决json中文乱码 springMvc解决json中文乱码,springMvc中文乱码,spring中文乱码 >>>>>>>>> ...

随机推荐

  1. NOIP2013题解

    NOIP2013题解 Day1 转圈游戏 circle 快速幂模板题. #include<iostream> using namespace std; int n,m,k,x; int f ...

  2. luogu2149 Elaxia的路线 (dijkstra+拓扑dp)

    先标记上一个人所有最短路上的边(同时也要标记反向边) 然后拿着另一个人最短路上的边(会构成一个DAG)去做拓扑dp,记从原点到某个点的最大的某个路径的被标记的边的个数 #include<bits ...

  3. 使用“DiskGenius”精确隐藏硬盘坏道

    现在大家手中可能都有些有坏道的硬盘,也可能现在机器上的硬盘也出问题了.硬盘有坏道,肯定不会全部都是坏道,不能使用了.但我们因此而不能使用了,那么就太可惜了.所以,只要把有坏道的区域隐藏起来,就如同使用 ...

  4. 洛谷P2680 运输计划

    大概就是二分+树上差分... 题意:给你树上m条路径,你要把一条边权变为0,使最长的路径最短. 最大的最小,看出二分(事实上我并没有看出来...) 然后二分k,对于所有大于k的边,树上差分求出最长公共 ...

  5. 将文件转换为base64字符串,然后还原

    package com.um.banks.xinlian.utils; import java.io.File; import java.io.FileInputStream; import java ...

  6. 斯坦福大学公开课机器学习:梯度下降运算的学习率a(gradient descent in practice 2:learning rate alpha)

    本章节主要讲怎么确定梯度下降的工作是正确的,第二是怎么选择学习率α,如下图所示: 上图显示的是梯度下降算法迭代过程中的代价函数j(θ)的值,横轴是迭代步数,纵轴是j(θ)的值 如果梯度算法正常工作,那 ...

  7. linux command ------ tar

    -c: compress archives -x:decompress archives -t:check archives -z:whether it has the attribute of gz ...

  8. python备份网站,并删除指定日期文件

    #!/usr/bin/python# Filename: backup_ver1.pyimport osimport timeimport datetime# 1. The files and dir ...

  9. Java连接访问Oracle--Connection.setSavepoint()方法使用

    使用时有一个重要前提:你不能使用oracle的classes12.jar,需要把oracle的jdbc驱动替换成ojdbc14.jar,否则savepoint()功能不能使用(出现“abstract方 ...

  10. 浅谈js的数字格式

    除了正常我们常用的十进制(如5,8,12.123等),js还可以直接表示2.8.16进制 1.二进制 二进制是以0b开头 0b10; 2.八进制 八进制是以0开头 010: 3.十六进制 十六进制是以 ...