• 对象中的字段隐藏


    工作中遇到了一个情况,一个对象里面的字段挺多的,但是每次调用接口只会用到其中的部分字段,全传过去前端渲染就会花很久,然后就会很卡,这个时候就需要将对象中为空的字段隐藏掉,保证给前端的对象中只有有值的字段
    这个时候就可以用这个注释

    @JsonInclude(JsonInclude.Include.NON_NULL)
    
    • 1

    点进去这个Include枚举

        public enum Include
        {
            /**
             * Value that indicates that property is to be always included,
             * independent of value of the property.
             */
             //始终显示所有字段
            ALWAYS,
    
            /**
             * Value that indicates that only properties with non-null
             * values are to be included.
             */
             //只显示非空的字段
            NON_NULL,
    
            /**
             * Value that indicates that properties are included unless their value
             * is:
             *
      *
    • null
    • *
    • "absent" value of a referential type (like Java 8 `Optional`, or * {link java.util.concurrent.atomic.AtomicReference}); that is, something * that would not deference to a non-null value. *
    * This option is mostly used to work with "Optional"s (Java 8, Guava). * * @since 2.6 */
    //排除空值和“不存在”的值。此处的不存在值表示引用空值的非空引用类型值 NON_ABSENT, /** * Value that indicates that only properties with null value, * or what is considered empty, are not to be included. * Definition of emptiness is data type specific; see below * for details on actual handling. *

    * Default emptiness for all types includes: *

      *
    • Null values.
    • *
    • "Absent" values (see {@link #NON_ABSENT})
    • *
    * so that as baseline, "empty" set includes values that would be * excluded by both {@link #NON_NULL} and {@link #NON_ABSENT}. *
    * Beyond this base, following types have additional empty values: *
      *
    • For {@link java.util.Collection}s and {@link java.util.Map}s, * method isEmpty() is called; *
    • *
    • For Java arrays, empty arrays are ones with length of 0 *
    • *
    • For Java {@link java.lang.String}s, length() is called, * and return value of 0 indicates empty String *
    • *
    * and for other types, null values are excluded but other exclusions (if any). *

    * Note that this default handling can be overridden by custom * JsonSerializer implementation: if method isEmpty() * is overridden, it will be called to see if non-null values are * considered empty (null is always considered empty). *

    * Compatibility note: Jackson 2.6 included a wider range of "empty" values than * either earlier (up to 2.5) or later (2.7 and beyond) types; specifically: *

      *
    • Default values of primitive types (like 0 for `int`/`java.lang.Integer` * and `false` for `bool`/`Boolean`) *
    • *
    • Timestamp 0 for date/time types *
    • *
    * With 2.7, definition has been tightened back to only containing types explained * above (null, absent, empty String, empty containers), and now * extended definition may be specified using {@link #NON_DEFAULT}. */
    //只显示非空和不是空字符串的字段 NON_EMPTY, /** * Meaning of this setting depends on context: whether annotation is * specified for POJO type (class), or not. In latter case annotation * is either used as the global default, or as property override. *

    * When used for a POJO, definition is that only values that differ from * the default values of POJO properties are included. This is done * by creating an instance of POJO using zero-argument constructor, * and accessing property values: value is used as the default value * by using equals() method, except for the case where property * has `null` value in which case straight null check is used. *

    * When NOT used for a POJO (that is, as a global default, or as property * override), definition is such that: *

      *
    • All values considered "empty" (as per {@link #NON_EMPTY}) are excluded
    • *
    • Primitive/wrapper default values are excluded
    • *
    • Date/time values that have timestamp (`long` value of milliseconds since * epoch, see {@link java.util.Date}) of `0L` are excluded
    • *
    */
    //排除具有POJO默认值的属性 NON_DEFAULT, /** * Value that indicates that separate `filter` Object (specified by * {@link JsonInclude#valueFilter} for value itself, and/or * {@link JsonInclude#contentFilter} for contents of structured types) * is to be used for determining inclusion criteria. * Filter object's equals() method is called with value * to serialize; if it returns true value is excluded * (that is, filtered out); if false value is included. * * @since 2.9 */ //自定义包含规则 //如果使用@JsonInclude#value=JsonInclude.Include.CUSTOM并通过@JsonInclude#value filter指定一个筛选器类,则仅当该属性值未被筛选器类筛选时,才会对其进行序列化。filter类的equals()方法用于筛选值;如果返回“true”,则不序列化值。 CUSTOM, /** * Pseudo-value used to indicate that the higher-level defaults make * sense, to avoid overriding inclusion value. For example, if returned * for a property this would use defaults for the class that contains * property, if any defined; and if none defined for that, then * global serialization inclusion details. * * @since 2.6 */ //排除使用POJO默认值的属性 USE_DEFAULTS ; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
  • 相关阅读:
    必知必会Java命令-jps
    HTML5期末考核大作业,网站——青岛民俗 7页。 美丽家乡 学生旅行 游玩 主题住宿网页
    图解 LeetCode 算法汇总——双指针
    猿创征文|School StartsFirstProject~UnityVR(HTCVive设备开发)
    IF:14+ “冒烟型”骨髓瘤的分子组成突显了导致多发性骨髓瘤的进化途径
    计算机毕业设计ssm+vue基本微信小程序的疫情监控系统
    聊聊大数据框架的数据更新解决方案: COW, MOR, MOW
    java:判断字符串是否为数字
    森林野火故事2.0:一眼看穿!使用 Panel 和 hvPlot 可视化 ⛵
    《LeetCode力扣练习》代码随想录——数组(长度最小的子数组---Java)
  • 原文地址:https://blog.csdn.net/NewBeeMu/article/details/126509630