有时候我们发现接收的是中文,返回却是个?。这确实是个蛋疼的问题,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 中文乱码与返回实体类报错的更多相关文章

  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. POJ3122-Pie-二分答案

    有N个派,F+1个人,每个人分到的体积要相等,而且每个人只能有一块派. 二分答案,对于一个mid,对每个派进行检测,尽量的多分,然后如果份数比F+1大,说明mid可以更大,就把mid给low.注意份数 ...

  2. 【 HDU1081 】 To The Max (最大子矩阵和)

    题目链接 Problem - 1081 题意 Given a two-dimensional array of positive and negative integers, a sub-rectan ...

  3. php laravel 多条件筛选

    效果如图,点击的条件出现在已选择的地方,点击已选择的条件可以删除当前点击的条件 语言是php 框架是laravel. 一.html <div class="doctor-conditi ...

  4. 自学Zabbix之路15.3 Zabbix数据库表结构简单解析-Triggers表、Applications表、 Mapplings表

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix之路15.3 Zabbix数据库表结构简单解析-Triggers表.Applica ...

  5. ORMLite学习入门笔记

    ORMLite学习入门笔记 使用原始的SQLiteHelper来操作维护数据库有点过于繁琐,重复工作量较大.所以会想到使用一个比较方便的ORM来维护我们本地的数据库,各位业界前辈都给我推荐了ORMLi ...

  6. 【洛谷P1854】花店橱窗 线性dp+路径输出

    题目大意:给定 N 个数字,编号分别从 1 - N,M 个位置,N 个数字按照相对大小顺序放在 M 个位置里,每个数放在每个位置上有一个对答案的贡献值,求一种摆放方式使得贡献值最大. 题解:一道典型的 ...

  7. Vetur:VSCode下强大的Vue开发工具

    Vue自从发布以来就一直受到前端开发人员的热爱,而除了一些本身优秀的特征之外,还有就是丰富的插件和工具的支持和更新.虽然现在的编辑器需要安装相应的plugins才能进行Vue组件化开发,但是越来越多优 ...

  8. myeclipse设置,提高开发效率

    全局搜索,打开一个文件,却把另外一个给关掉. 解决办法: 在myeclipse中window-preferences-general-search找到第一行的一个选项reuse editors to ...

  9. 注解 和 xml 配置的优缺点【转】

    java annotation(注解) 的优点缺点 Annotation和xml各自作为配置项的优点与缺点. Annotation 一.Annotation 的优点 1.保存在 class 文件中,降 ...

  10. Pool多进程示例

    利用Pool类多进程实现批量主机管理 #!/usr/bin/python # -*- coding: UTF-8 -*- # Author: standby # Time: 2017-03-02 # ...