SpringMVC解决@ResponseBody注解返回中文乱码

本文介绍了解决SpringMVC项目中@ResponseBody返回中文数据出现乱码的问题。通过配置RequestMappingHandlerAdapter来统一设置字符编码,避免了在每个@RequestMapping中单独指定produces属性。

今天做项目的时候,在使用SpringMVC的@ResponseBody的时候,发现返回网页的中文数据是乱码。

   @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String test(){
        return "中文";
    }

我使用SpringMVC使用的版本是5.0.7.RELEASE,后来网上找了一些资料,在@RequestMapping里强制指定返回的数据类型和字符编码,中文乱码解决,如下:  

@RequestMapping(value="/test",produces = "application/json; charset=utf-8")  

在spring处理ResponseBody时涉及到org.springframework.http.converter.StringHttpMessageConverter这个类,该类在默认实现中将defaultCharset设为ISO-8859-1。当@RequestMapping标记的方法未配置produces属性时,将自动使用默认编码;如果配置了produces属性,AbstractHttpMessageConverter中的write方法将不会受supportedMediaTypes影响,而用produce设置的header赋值给contenttype。改造一下RequestMappingHandlerAdapter的配置,springMvc.xml如下:

注意:基于spring3.2以后的配置文件。编码配置需要放在<mvc:annotation-driven/>之前,否则无效。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <context:component-scan base-package="pers.kevin.mvcrest"/>

    <context:annotation-config />

   <!-- 必须放在<mvc: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/plain;charset=UTF-8</value>
                            <value>text/html;charset=UTF-8</value>
                            <value>applicaiton/javascript;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>

   
            </list>
        </property>
    </bean>



    <mvc:annotation-driven/>

    <mvc:resources location="public" mapping="/**"/>





    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

 

 上述springMvc.xml文件重新设置了StringHttpMessageConverter的编码方式,而不必在每个@RequestMapping里设置produces属性。如果返回的Jackson类型的数据,可以设置成如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <context:component-scan base-package="pers.kevin.mvcrest"/>

    <context:annotation-config />


    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json; charset=UTF-8</value>
                        <value>application/x-www-form-urlencoded; charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            </list>
        </property>
    </bean>



    <mvc:annotation-driven/>


    <mvc:resources location="public" mapping="/**"/>





    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

查阅的资料,想了解更多可以查看下面的链接:

springMVC的消息转换器(Message Converter)

SpringMVC使用注解@ResponseBody返回json以及中文乱码问题解决

Spring MVC @ResponseBody响应中文乱码

【SpringMVC】解决@ResponseBody注解返回中文乱码

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值