zoukankan      html  css  js  c++  java
  • @JsonInclude(Include.NON_NULL)

    原文地址:https://www.cnblogs.com/-xuzhankun/p/8034179.html

    前端的同事要求说尽量不要有null,可有为空串“” 或者 0 或者 [], 但尽量不要null。

      所以@JsonInclude(Include.NON_NULL) 这个注解放在类头上就可以解决。 实体类与json互转的时候 属性值为null的不参与序列化

      

    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonInclude.Include;

    @JsonInclude(Include.NON_NULL)
    public class WithdrawDetail implements Serializable {

    }

    或者

    WithdrawDetail wd = new WithdrawDetail();

    wd.setSerializationInclusion(Include.NON_NULL);

      实际效果

    全局配置

    springMVC.xml

    <!-- 默认的注解映射的支持 比如requestMapper之类的 -->
    <mvc:annotation-driven>
    <mvc:message-converters>
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="objectMapper">
    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
    <property name="serializationInclusion"> 
    <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value> 
    </property>
    </bean>
    </property>
    </bean>
    </mvc:message-converters>
    </mvc:annotation-driven>

    --------------spring boot 的配置

    只在配置文件加上一个配置

    spring.jackson.default-property-inclusion=non_null

  • 相关阅读:
    [BZOJ3530][SDOI2014]数数
    [Luogu3121][USACO15FEB]审查Censoring
    [BZOJ1212][HNOI2004]L语言
    [Luogu3041][USACO12JAN]视频游戏的连击Video Game Combos
    AC自动机总结
    (三)LDAP 新增用户
    (二) LDAP 安装
    (一)LDAP 简介
    (十三)VMware Harbor 身份验证模式
    loj#2541. 「PKUWC2018」猎人杀
  • 原文地址:https://www.cnblogs.com/eyesfree/p/10132954.html
Copyright © 2011-2022 走看看