V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
eightqueen
V2EX  ›  Java

spring4 mvc rest 返回中文乱码?

  •  1
     
  •   eightqueen · 2015-06-04 17:40:48 +08:00 · 8436 次点击
    这是一个创建于 3242 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目前是通过在每个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
    类似这种配置有很多,但是都不起作用.

    14 条回复    2015-06-05 23:48:47 +08:00
    loveyu
        1
    loveyu  
       2015-06-04 17:49:21 +08:00 via Android
    不知道你指的是何种乱码,如果内容正确但乱码(浏览器改下就好)这种,你只需要header中输出时指定编码就好,其他的就不知道
    hicfpx
        2
    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
        3
    ngloom  
       2015-06-04 17:56:59 +08:00
    用的spring boot,@RestController 好像木有这个问题。。。
    saximoer
        4
    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
        5
    zava  
       2015-06-04 18:09:27 +08:00
    想一劳永逸的话, 直接配置一个 charset 的 filter 吧:
    org.springframework.web.filter.CharacterEncodingFilter
    vjnjc
        6
    vjnjc  
       2015-06-04 20:28:12 +08:00
    哈哈哈,也是mvc:message-converters ,这货绝对坑,据说是spring framework里面一句源码里指定了编码,所以用spring 框架转得json中文都会乱码。
    记得我那次是produces = {"application/json;charset=UTF-8"}有效地,然而xml配置无效
    vikeria
        7
    vikeria  
       2015-06-04 21:08:58 +08:00
    5楼加一
    varrily
        8
    varrily  
       2015-06-05 10:07:13 +08:00
    CharacterEncodingFilter spring3就这么解决的
    eightqueen
        9
    eightqueen  
    OP
       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
        10
    Alexisused  
       2015-06-05 13:44:23 +08:00
    spring 这个确实很坑 经常遇到这个问题 现在也没搞清楚到底什么原因
    phx13ye
        11
    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
        12
    eightqueen  
    OP
       2015-06-05 14:50:58 +08:00
    @phx13ye 已经改成restcontroller,这个不能怪浏览器吧,毕竟用户不会去改浏览器编码的
    phx13ye
        13
    phx13ye  
       2015-06-05 15:06:42 +08:00
    @eightqueen 浏览器直接去访问这个requestmapping的方法吗?

    仅指定produces = {"application/json}我用ajax取数据然后js生成了一些东西也没有这个问题
    eightqueen
        14
    eightqueen  
    OP
       2015-06-05 23:48:47 +08:00
    @phx13ye produces = {"application/json}这个是好用的,我希望有个全局配置
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   899 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:08 · PVG 05:08 · LAX 14:08 · JFK 17:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.