spring4 mvc rest 返回中文乱码?

2015-06-04 17:40:48 +08:00
 eightqueen

目前是通过在每个RequestMapping注解中加入produces = {"application/json;charset=UTF-8"}来解决.但是我希望通过配置一劳永逸,在网上搜索了一下,
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
/mvc:message-converters
/mvc:annotation-driven
类似这种配置有很多,但是都不起作用.

8443 次点击
所在节点    Java
14 条回复
loveyu
2015-06-04 17:49:21 +08:00
不知道你指的是何种乱码,如果内容正确但乱码(浏览器改下就好)这种,你只需要header中输出时指定编码就好,其他的就不知道
hicfpx
2015-06-04 17:55:09 +08:00
你返回的是json,就需要配置json converter吧

<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
<property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
</bean>
<!-- 将Jackson2HttpMessageConverter的默认格式化输出设为true -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="prettyPrint" value="true"/>
<property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
ngloom
2015-06-04 17:56:59 +08:00
用的spring boot,@RestController 好像木有这个问题。。。
saximoer
2015-06-04 18:07:39 +08:00
```
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<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>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
```

不知道这个能不能帮到?
zava
2015-06-04 18:09:27 +08:00
想一劳永逸的话, 直接配置一个 charset 的 filter 吧:
org.springframework.web.filter.CharacterEncodingFilter
vjnjc
2015-06-04 20:28:12 +08:00
哈哈哈,也是mvc:message-converters ,这货绝对坑,据说是spring framework里面一句源码里指定了编码,所以用spring 框架转得json中文都会乱码。
记得我那次是produces = {"application/json;charset=UTF-8"}有效地,然而xml配置无效
vikeria
2015-06-04 21:08:58 +08:00
5楼加一
varrily
2015-06-05 10:07:13 +08:00
CharacterEncodingFilter spring3就这么解决的
eightqueen
2015-06-05 10:54:15 +08:00
@zava
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
你说的是这个吗?已经配过了
Alexisused
2015-06-05 13:44:23 +08:00
spring 这个确实很坑 经常遇到这个问题 现在也没搞清楚到底什么原因
phx13ye
2015-06-05 14:26:50 +08:00
controler改成restcontroler,这个只是浏览器编码问题吧
restcontroler会自动添加这个
14:23:36,148 DEBUG [qtp1471230198-22] annotation.RequestResponseBodyMethodProcessor:163 - Written [Person(id=1, name=返回中文, age=23)] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@450281f6]
eightqueen
2015-06-05 14:50:58 +08:00
@phx13ye 已经改成restcontroller,这个不能怪浏览器吧,毕竟用户不会去改浏览器编码的
phx13ye
2015-06-05 15:06:42 +08:00
@eightqueen 浏览器直接去访问这个requestmapping的方法吗?

仅指定produces = {"application/json}我用ajax取数据然后js生成了一些东西也没有这个问题
eightqueen
2015-06-05 23:48:47 +08:00
@phx13ye produces = {"application/json}这个是好用的,我希望有个全局配置

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/196131

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX