Spring MVC JSON乱码问题
之前项目中也遇到过返回JSON时乱码问题,当时找到了一个方法解决了问题但是没有明白原因,今天这个项目又遇到了JSON乱码问题,用之前的方法不行,看了这篇博文才明白为什么
@RequestMapping的produces方法
- @RequestMapping(value = “testPersonalValidtor.do”,produces = “application/json;charset=utf-8”)
@RequestMapping(value = "testPersonalValidtor.do",produces = "application/json;charset=utf-8")
在方法上加上这个注解就可以了。但是这样写的话有限制,只能在特定的方法上面使用。如果需要全局都使用的话,需要修改SpringMVC的配置文件。
使用messageConverters
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=utf-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
并且需要在Maven依赖中配置上Jackjson的依赖。
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
使用<mvc:message-converters>
<!-- SpringMVC注解驱动 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=utf-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
注意:始用这种配置的时候,需要去掉RequestMappingHandlerMapping、RequestMappingHandlerAdapter或者DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter的Bean配置,要不然可能会不生效。
@RequestMapping(value = "testPersonalValidtor.do")
@ResponseBody
//直接返回对象
public Object testPersonalValidtor(@Valid PersonScope personScope, BindingResult bindingResult){
if(bindingResult.hasErrors()){
StringBuffer sb = new StringBuffer();
for(ObjectError objectError : bindingResult.getAllErrors()){
sb.append(((FieldError)objectError).getField() +" : ").append(objectError.getDefaultMessage());
}
return sb.toString();
}else{
return personScope;
}
}
参考链接:https://blog.csdn.net/porsche_gt3rs/article/details/79062704
Spring MVC JSON乱码问题的更多相关文章
- spring mvc json乱码
<mvc:annotation-driven> <mvc:message-converters> <!-- 将StringHttpMessageConverter的默认编 ...
- spring mvc json 返回乱码问题解决(vestion:3.x.x)
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<spring mvc json 返回乱码问题解决(vestion:3.x.x)> 工程中用springmvc返 ...
- spring mvc: json练习
spring mvc: json练习 本例需要用到的json包: 如下: jackson-databind jackson-core jackson-annotations <!-- https ...
- Spring MVC JSON自己定义类型转换(续)
前面提到了两种转换类型的方法(Spring MVC JSON自己定义类型转换),这里针对Json转换提供一种更简便的方法. 通过配置全局的日期转换来避免使用麻烦的注解. 首先用到了一个简单的日期工具类 ...
- spring mvc json返回防止乱码
乱码问题 乱码一直是编程的常见问题,spring mvc 返回json数据时可能导致乱码,需要在controller中添加如下代码: @RequestMapping("/test" ...
- spring mvc 返回乱码SpringMVC使用@ResponseBody注解返回中文字符串乱码的问题
原文地址:https://www.cnblogs.com/fzj16888/p/5923232.html 先说一下我的经历,以及解决问题的而过程. 在使用SpringMVC的时候,最开始的时候在配置文 ...
- [Spring MVC] - JSON
Spring MVC中使用JSON,先必需引用两个包:jackson-core-asl-1.9.13.jar.jackson-mapper-asl-1.9.13.jar 因为需要使用到jquery测试 ...
- spring mvc 中文乱码 post与get的方法解决
spring mvc表单提交中文参数乱码问题 今天测试spring mvc ,中文乱码,在web.xml中加上 <filter> <filter-name>encodingF ...
- Spring MVC JSON 实现JsonSerializer Date类型转换
转载至:http://blog.csdn.net/lantianzhange/article/details/40920933 在Spring MVC中存在两大类的类型转换,一类是Json,一个是Sp ...
随机推荐
- js 数组去重求和 (转载)
方法一:js数组id去重,value值相加问题 来源:https://www.jianshu.com/p/8f79e31b46ed // js let arr = [ { id: 1, value: ...
- 身份证验证PHP类
PHP根据身份证号,自动获取对应的星座函数,然后自动返回对应的星座,自动返回性别,判断是否成年 <?php class IdcardAction extends Action{ // PHP根据 ...
- 复盘一篇浅谈KNN的文章
认识-什么是KNN KNN 即 K-nearest neighbors, 是一个hello world级别, 但被广泛使用的机器学习算法, 中文叫K近邻算法, 是一种基本的分类和回归方法. KNN既可 ...
- CentOS7安装VNC
#安装 yum -y install tigervnc-server 将配置表复制到etc .service 修改配置文件 vim /etc/systemd/system/vncserver@\:.s ...
- 百度语音合成api/sdk及demo
1.流程 1)换取token 用Api Key 和 SecretKey.访问https://openapi.baidu.com/oauth/2.0/token 换取 token // appKey = ...
- bat echo 每行不同的颜色
bat echo 每行不同的颜色 先看代码: @echo off SETLOCAL EnableDelayedExpansion for /F "tokens=1,2 delims=#&qu ...
- P4160 [SCOI2009]生日快乐[dfs]
题目描述 windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕. 现在包括windy,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的蛋糕. win ...
- assert 断言
输入 assert 1>2,'123' 输出结果 assert 1>2,'123' AssertionError: 123
- Python通过xpath查找元素通过selenium读取元素信息
#coding:utf-8 from selenium import webdriver import time url ='http://www.baidu.com' driver = webdri ...
- test20190905 ChiTongZ
100+22+90=212.前两道题不错,但T3 没什么意义. 围观刘老爷超强 T1 解法. ChiTongZ的水题赛 [题目简介] 我本可以容忍黑暗,如果我不曾见过太阳. 考试内容略有超纲,不超纲的 ...