springmvc 自定義responseBody消息轉(zhuǎn)換

 <?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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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.xsd">

    <context:component-scan base-package="ucloud.v4.web"/>

    <bean id="myValueFilter" class="ucloud.v4.common.MyValueFilter"/>

    <mvc:annotation-driven/>
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <property name="fastJsonConfig">
                    <bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
                        <property name="serializerFeatures">
                            <list>
                                <!--是否輸出值為null的字段,默認(rèn)為false-->
                                <value>WriteMapNullValue</value>
                                <!--List字段如果為null,輸出為[],而非null-->
                                <value>WriteNullListAsEmpty</value>
                                <!--字符類型字段如果為null,輸出為”“,而非null-->
                                <value>WriteNullStringAsEmpty</value>
                                <!--數(shù)值字段如果為null,輸出為0,而非null-->
                                <value>WriteNullNumberAsZero</value>
                                <!--Boolean字段如果為null,輸出為false,而非null-->
                                <value>WriteNullBooleanAsFalse</value>
                                <!--按字段名稱排序后輸出。默認(rèn)為false-->
                                <value>SortField</value>
                            </list>
                        </property>
                        <property name="serializeFilters">
                            <list>
                                <ref bean="myValueFilter"/>
                            </list>
                        </property>
                    </bean>
                </property>
            </bean>
            <!--<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">-->
                <!--<property name="supportedMediaTypes">-->
                    <!--<list>-->
                        <!--<value>text/html;charset=UTF-8</value>-->
                    <!--</list>-->
                <!--</property>-->
                <!--<property name="objectMapper">-->
                    <!--<bean class="ucloud.v4.common.MyJacsonSerializer"/>-->
                <!--</property>-->
            <!--</bean>-->
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>
public class MyValueFilter implements ValueFilter{
    @Override
    public Object process(Object o, String s, Object o1) {
        if (o1 == null)
            return "";
        return o1;
    }
}

阿里關(guān)于SerializeFilter的文檔
https://github.com/alibaba/fastjson/wiki/SerializeFilter

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容