Springmvc使用阿里巴巴的fastjson传输到前台中文乱码的解决方案,他娘的大家都少制造垃圾,学习过程将会多么快乐

弄了大概七八个小时吧

都他妈比的抄来抄去,一分一秒的去试错

最终参考这个问题解决了乱码的情况https://bbs.csdn.net/topics/392169549

412

需要在springmvc中添加如下配置(我都使用的utf-8)

  <!-- string乱码 -->
    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <constructor-arg value="utf-8" index="0"/>
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;charset=utf-8</value>
            </list>
        </property>
    </bean>
 
    <!-- fastJson配置 -->
    <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/json;charset=utf-8</value>
                <value>text/html;charset=utf-8</value>
            </list>
        </property>
    </bean>
 
    <!-- 请求处理器 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <ref bean="fastJsonHttpMessageConverter"/>
            <ref bean="stringHttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!--fastJsonConfig -->
    <bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
        <!--默认编码格式 -->
        <property name="charset" value="UTF-8"/>

        <property name="serializerFeatures">
            <list>
                <value>WriteNullListAsEmpty</value>
                <value>WriteDateUseDateFormat</value>
                <value>PrettyFormat</value>
                <value>WriteMapNullValue</value>
                <value>WriteNullStringAsEmpty</value>
                <value>WriteNullListAsEmpty</value>
                <value>DisableCircularReferenceDetect</value>
            </list>
        </property>

    </bean>
    <!--fastjson支持配置结束 -->

 控制器这样写

如果有

produces= "application/json;charset=UTF-8"

这句话

produces= {"application/json;charset=UTF-8"}

返回数据如下

如果没有上边那句话

就成了这样的

太晚了,睡觉,明天在研究

乱码问题解决,别的就都好说了

猜你喜欢

转载自www.cnblogs.com/jnhs/p/10176673.html