Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fastjson与Spring MVC 整合后配置WriteMapNullValue怎么让一部分类序列化后不输出null #946

Open
Bobi-zhou opened this issue Dec 9, 2016 · 10 comments

Comments

@Bobi-zhou
Copy link

在Spring mvc中配置为

<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
    <!-- Default charset -->
    <property name="charset" value="UTF-8" />
    <!-- SerializerFeature -->
    <property name="serializerFeatures">
        <list>
            <value>QuoteFieldNames</value>
            <value>WriteMapNullValue</value>
        </list>
    </property>
</bean>
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
         <!--将StringHttpMessageConverter的默认编码设为UTF-8-->
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8"/>
        </bean>
        <!-- 配置Fastjson支持 -->
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json</value>
                </list>
            </property>
            <property name="fastJsonConfig" ref="fastJsonConfig"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

这样配置后,Controller返回的对象属性值为空会输出{"name":null} ,怎么让一部分类序列化输出忽略null

使用jackson时默认输出null,但是在类上加上注解@JsonInclude(JsonInclude.Include.NON_NULL)就可以实现目的,但是在fastjson中没有找到相关配置。

@CharkeyQK
Copy link
Contributor

@Bobi-zhou
Copy link
Author

使用@JSONField(serialize=false)后不管该字段值是否为Null都不会被序列化,我的要求是当字段的值为Null时 不序列化,当不为Null时 需要序列化。

@Bobi-zhou
Copy link
Author

@CharkeyQK 使用你说的方法可以解决自己手动调用API序列化的情况,但是怎么与Spring MVC整合起来,让Sping MVC 知道我哪些类 序列化的时候要忽略Null值

@CharkeyQK
Copy link
Contributor

CharkeyQK commented Dec 23, 2016

com.alibaba.fastjson.support.config.FastJsonConfig#classSerializeFilters

Spring MVC 配置 fastJsonHttpMessageConverter 的时候,也配置一下 fastJsonConfig,其中 fastJsonConfig 可以配置 classSerializeFilters。

public class NullValuePropertyFilter implements PropertyFilter {

    @Override
    public boolean apply(Object object, String name, Object value) {
        return value != null;
    }
}

@Bobi-zhou
Copy link
Author

@CharkeyQK 这样配置后应该会覆盖WriteMapNullValue属性,难道要在apply方法中判断是否是某个特定的类?这样显得不太灵活,后面新增了有这样需求的类,得再来修改apply方法,我希望能设计一个注解,该注解的优先级大于Sping MVC 中fastJsonConfig的配置,这样在我不希望序列化Null值的类上,我只需要注解一下就ok

@CharkeyQK
Copy link
Contributor

用 PropertyFilter,apply 方法中获取 类上面的注解,然后做你需要的逻辑判断。

@chongyangxue
Copy link

classSerializeFilters 是一个Map<Class<?>, SerializeFilter>,只能给具体的类加过滤器,如果想通过@CharkeyQK 说的注解的方式给所有的bean都加上PropertyFilter怎么做呢?

@liuguangjun
Copy link

同问

@Omega-Ariston
Copy link
Collaborator

#2684 应该就可以解决你的问题,在下一版本中将会实现这一个功能。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants