Spring @responsebody返回json字符串乱码 or 请求406
来源:互联网 发布:数据库与区块链对比 编辑:程序博客网 时间:2024/05/16 10:10
- 由于项目中每个人喜欢返回的东西不一样,有的人返回json字符串,有的就直接@response注解返回对应的类型。但是Spring对字符串转换和对象转换使用不同转换器。String转换器,默认转换编码ISO-8859-1,
对象转换器默认转编码utf-8。 - 出现http响应吗406,一种可能是我们没有配置对象转换器,还有可能就是缺少了对应的转换的jackson包。 启用
@Responsebody注解 将采用默认的转换器。
/** * Adds a set of default HttpMessageConverter instances to the given list. * Subclasses can call this method from {@link #configureMessageConverters(List)}. * @param messageConverters the list to add the default message converters to */ protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) { StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(); stringConverter.setWriteAcceptCharset(false); messageConverters.add(new ByteArrayHttpMessageConverter()); messageConverters.add(stringConverter); messageConverters.add(new ResourceHttpMessageConverter()); messageConverters.add(new SourceHttpMessageConverter<Source>()); messageConverters.add(new AllEncompassingFormHttpMessageConverter()); if (romePresent) { messageConverters.add(new AtomFeedHttpMessageConverter()); messageConverters.add(new RssChannelHttpMessageConverter()); } if (jaxb2Present) { messageConverters.add(new Jaxb2RootElementHttpMessageConverter()); } if (jackson2Present) { messageConverters.add(new MappingJackson2HttpMessageConverter()); } else if (jacksonPresent) { messageConverters.add(new MappingJacksonHttpMessageConverter()); } }
查看源码发现启用注解有很多类型的转换器。StringHttpMessageConverter类型的转换器默认编码
所以当返回String类型的json字符串,只启用注解的话就会乱码。
解决方法
- 在配置文件中配置StringHttpMessageConverter
<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>application/json;charset=UTF-8</value> <value>text/plain;charset=UTF-8 </value> </list> </property> </bean> </list> </property> </bean>
注解必须放在配置之后。否则启用的还是默认的转换器。如果你还想配置对象转换器还需要在加MappingJacksonHttpMessageConverter
<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>application/json;charset=UTF-8</value> <value>text/plain;charset=UTF-8 </value> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean>
当然你也可以注解只用对象转化器因为面向对象嘛。
- 参考博客
Spring messageConverters
0 0
- Spring @responsebody返回json字符串乱码 or 请求406
- spring controller返回json字符串 @responsebody 乱码
- SpringMVC ResponseBody返回字符串(JSON)乱码
- Spring Mvc @ResponseBody返回json乱码
- SpringMVC3的ResponseBody返回字符串(JSON)乱码问题解决
- 解决spring-mvc @responseBody注解返回json 乱码问题
- Spring 3.0.5 @ResponseBody 返回json中文乱码
- 解决spring-mvc @responseBody注解返回json 乱码问题
- Spring mvc @ResponseBody返回JSON前台JSP乱码解决
- 解决spring-mvc @responseBody注解返回json 乱码问题
- spring中用@responsebody返回json时出现乱码问题
- 解决spring-mvc @responseBody注解返回json 乱码问题
- 解决spring-mvc @responseBody注解返回json 乱码问题
- 解决spring-mvc @responseBody注解返回json 乱码问题
- 解决spring-mvc @responseBody注解返回json 乱码问题
- SpringMVC@ResponseBody返回JSON乱码
- Spring mvc使用注解@ResponseBody Ajax请求返回json报406错误
- Spring MVC使用@ResponseBody返回JSON数据406以及乱码问题解决方案
- 全编全解转码和部分编解码
- scala + intellij idea 环境搭建及编译、打包
- cf 742B Arpa's......
- 打印iPhone系统自带的字体列表
- python 安装 paramiko
- Spring @responsebody返回json字符串乱码 or 请求406
- Using Alamofire from Objective-C
- 在线申贷前端后台实现二(插入数据库)
- 项目经理必备7要素
- java8 Lambda表达式详解
- 欢迎使用CSDN-markdown编辑器
- Android动画之AlphaAnimation
- 对javav的一些起源认识!
- 操作系统Unix、Windows、Mac OS、Linux的故事 科普篇