【坑】SpringMvc 处理JSON 乱码

文章目录


前言

在使用 springMvc 的时候,如果向前台返回 JSON 数据,JSON 中的中文会乱码;

即使你在配置了全局的信息编码拦截器,也无济于事;

原因大抵是,JSON 的内部方法,使用的是 ISO-8859 的硬编码,导致解析中文出错;


方法

Mvc 的配置文件里面,配置使用注解的处理器、适配器的地方,配置下 json 编码 ;

<!-- 使用基于注解的 处理器映射器和处理器适配器  -->
    <!-- validator :使用检验器-->
    <mvc:annotation-driven validator="validator" conversion-service="conversionService">
        <mvc:message-converters>
            <!-- 处理请求返回json字符串的中文乱码问题 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
原文地址:https://www.cnblogs.com/young-youth/p/11665575.html