springmvc 自定义responseBody消息转换

 <?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的字段,默认为false-->
                                <value>WriteMapNullValue</value>
                                <!--List字段如果为null,输出为[],而非null-->
                                <value>WriteNullListAsEmpty</value>
                                <!--字符类型字段如果为null,输出为”“,而非null-->
                                <value>WriteNullStringAsEmpty</value>
                                <!--数值字段如果为null,输出为0,而非null-->
                                <value>WriteNullNumberAsZero</value>
                                <!--Boolean字段如果为null,输出为false,而非null-->
                                <value>WriteNullBooleanAsFalse</value>
                                <!--按字段名称排序后输出。默认为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;
    }
}

阿里关于SerializeFilter的文档
https://github.com/alibaba/fastjson/wiki/SerializeFilter

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容