Spring MVC 4.3.3中的响应体编码

易天明*_*易天明 6 spring spring-mvc

要在spring-webmvc中设置@responsebody编码,我曾在配置文件中添加以下行:

<mvc:annotation-driven>
    <mvc:message-converters>
        <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>
        <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
    </mvc:message-converters>
Run Code Online (Sandbox Code Playgroud)

这将覆盖默认的charset responsebody处理程序使用。它与spring-mvc 4.2.7及更低版本一起使用。

但是,在最新版本的spring-webmvc(4.3.3)中,此方法不起作用。在新版本中,StringHttpMessageConverter从响应标头读取content-type,并且如果content-type字符串包含字符集信息,则它将使用此字符集并忽略其默认字符集。

我知道我可以这样写来解决这个问题:

@RequestMapping(value = "/getDealers", method = RequestMethod.GET, 
produces = "application/json; charset=utf-8")
@ResponseBody
public String sendMobileData() {

}
Run Code Online (Sandbox Code Playgroud)

但是我必须在每个方法或每个控制器上编写它。有没有像我以前那样全局设置响应主体编码的方法?

易天明*_*易天明 3

<value>application/json;charset=UTF-8</value>我发现我的配置中没有添加 。我不知道为什么我的旧配置适用于 4.2.7 及以下版本,但这个新配置仅适用于 4.3.3 版本:

<mvc:annotation-driven>
    <mvc:message-converters>
        <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>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
   </mvc:message-converters>
Run Code Online (Sandbox Code Playgroud)