SpringMVC使用注解@ResponseBody返回json以及中文乱码问题解决
2017-03-20 12:43
891 查看
SpringMVC使用注解@ResponseBody返回json以及中文乱码问题解决
在这里我不得支持一下SpringMVC是一个不错的框架,用了springmvc这么长时间 ,我犯了个错误,说来惭愧.
在项目中编码格式是UTF-8 ,在使用Ajax 请求, 在SpringMVC中返回Json 数据,我用到了@ResponseBody 注解 ,将表示该方法的返回结果直接写入HTTP response body中。
然后再从数据库中取出的中文通过Spring的response返回结果中文乱码.
方法一
原因:response在响应的时候 ,生成的response中"Content-Type"的值不正确。(默认的是ISO-8859-1)
然后Spring使用AnnotationMethodHandlerAdapter来处理@ResponseBody,该类再使用一些HttpMessageConverter来具体处理信息。
参考配置2:
参考配置3:(如果用的是Jackson框架)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/01/b4a99148240a72ea2f8856ea77d302dc)
----
基于spring3.2以后的配置文件。编码配置需要放在<mvc:annotation-driven/>之前,否则无效。
在SpringMVC 的Controller中使用@ResponseBody注解向客户端返回数据时,如果没有特殊设置则中文将显示为乱码,此时需要在Spring-MVC.xml配置文件中加入如下代码:
注意以上配置需要放到<mvc:annotation-driven
/>之前,否则无效。
=====
如果使用的是JSON-lib框架,则可以使用如下配置:
不断补充中...
在这里我不得支持一下SpringMVC是一个不错的框架,用了springmvc这么长时间 ,我犯了个错误,说来惭愧.
在项目中编码格式是UTF-8 ,在使用Ajax 请求, 在SpringMVC中返回Json 数据,我用到了@ResponseBody 注解 ,将表示该方法的返回结果直接写入HTTP response body中。
然后再从数据库中取出的中文通过Spring的response返回结果中文乱码.
方法一
原因:response在响应的时候 ,生成的response中"Content-Type"的值不正确。(默认的是ISO-8859-1)
然后Spring使用AnnotationMethodHandlerAdapter来处理@ResponseBody,该类再使用一些HttpMessageConverter来具体处理信息。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <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> <context:component-scan base-package="com.zdkj.limp.controller"></context:component-scan> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp"></property> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </beans>
参考配置2:
<mvc:annotation-driven conversion-service="conversionService"> <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" index="0"></constructor-arg> </bean> <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes" value="application/json;charset=UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven>
参考配置3:(如果用的是Jackson框架)
----
基于spring3.2以后的配置文件。编码配置需要放在<mvc:annotation-driven/>之前,否则无效。
在SpringMVC 的Controller中使用@ResponseBody注解向客户端返回数据时,如果没有特殊设置则中文将显示为乱码,此时需要在Spring-MVC.xml配置文件中加入如下代码:
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射,解决@ResponseBody乱码问题, 需要在annotation- driven之前,否则乱码问题同样无法解决 --> <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-lib框架,则可以使用如下配置:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <!-- 防止返回json乱码 --> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="text" /> <constructor-arg index="1" value="plain" /> <constructor-arg index="2" value="UTF-8" /> </bean> </list> </property> </bean> </list> </property> </bean>
不断补充中...
相关文章推荐
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- 解决springmvc使用ResponseBody注解返回json中文乱码问题
- 解决Springboot使用FastJson 返回中文乱码的问题
- Springboot使用FastJson后,接口返回中文乱码的问题解决。
- 解决springmvc返回json数据IE出现文件下载和json数据中文乱码问题
- springMvc框架解决返回Json格式中文乱码问题
- SSM框架:解决后台传数据到前台中文乱码问题,使用@ResponseBody返回json 中文乱码
- 解决springmvc使用@ResponseBody返回String类型字符串中文乱码问题
- SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.
- Springboot使用FastJson后,接口返回中文乱码的问题解决。
- Springboot使用FastJson后,接口返回中文乱码的问题解决(两种解决方式)
- springMVC_json格式数据的接收和发送(未解决中文乱码问题)
- Spring MVC后台返回JSON数据中文乱码问题解决
- 解决php中json中文乱码以及JQuery读取中文乱码的问题
- SpringMVC3的ResponseBody返回字符串(JSON)乱码问题解决
- springMVC_json格式数据的接收和发送(未解决中文乱码问题)
- springmvc使用@ResponseBody返回json乱码解决方法
- SpringMVC3.2 处理Ajax请求时返回json字符串的中文乱码问题
- Java学习笔记37:Spring 使用 @ResponseBody 返回中文乱码问题解决