SpringMVC4 Json自动转义

今天遇到了一个很神奇的坑,用了springmvc,用上restController做一个restful的接口,返回的字符串中的特殊字符被自动转义为unicode了。

跟踪进入springmvc的源代码,发现默认的messageconvert用的是gson,gson会自动进行转义,然后查下资料,改动了下配置文件

    <mvc:annotation-driven>

        <mvc:message-converters>

            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">

                <property name="supportedMediaTypes">

                    <list>

                        <value>application/json;charset=UTF-8</value>

                    </list>

                </property>

            </bean>

        </mvc:message-converters>

    </mvc:annotation-driven>

这个是用fastjson的方式,直接用MappingJackson2HttpMessageConverter替换就是用jackson来做序列化

猜你喜欢

转载自yanbo.iteye.com/blog/2297674