美文网首页
springmvc 自定义responseBody消息转换

springmvc 自定义responseBody消息转换

作者: DamagedBoy | 来源:发表于2017-06-30 00:06 被阅读0次
 <?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

相关文章

网友评论

      本文标题:springmvc 自定义responseBody消息转换

      本文链接:https://www.haomeiwen.com/subject/pntbcxtx.html