SpringBoot如何过滤掉Controller返回实体对象中的NULL或者空值的字段

懒驴 2021年11月18日 3,460次浏览

相信在SpringBoot的开发中,很多都有遇到过返回的对象中存在NULL或者空值的字段,如下图所示:
返回NULL示意图

对于这种情况,需要将空值字段去掉,那么解决方法如下:

使用SpringBoot中的注解:@JsonInclude,我们只需要在过滤的字段或者实体类上面加上
如下注解即可

@JsonInclude(JsonInclude.Include.ALWAYS.NON_NULL)

!注意 !!注意 !
注解参数选择

  1. 将该注解放在属性上,如果该属性为NULL则不参与序列化;
  2. 如果放在类上边,那对这个类的全部属性起作用;
  3. JsonInclude.Include.ALWAYS --默认,所有的属性都会被序列化
  4. JsonInclude.Include.NON_DEFAULT --属性为默认值不序列化
  5. JsonInclude.Include.NON_EMPTY --属性为空("")或者为 NULL 都不序列化
  6. JsonInclude.Include.NON_NULL --属性为NULL 不序列化
  7. JsonInclude.Include.CUSTOM --这个是自定义包含规则,详细见官方文档
  8. JsonInclude.Include.NON_ABSENT --这个包含NON_NULL,即为null的时候不序列化,详情看源码
  9. JsonInclude.Include.USE_DEFAULTS --使用默认值的情况下就不序列化

下面附上源码:
JsonInclude

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.fasterxml.jackson.annotation;

import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonInclude {
    JsonInclude.Include value() default JsonInclude.Include.ALWAYS;

    JsonInclude.Include content() default JsonInclude.Include.ALWAYS;

    Class<?> valueFilter() default Void.class;

    Class<?> contentFilter() default Void.class;

    public static class Value implements JacksonAnnotationValue<JsonInclude>, Serializable {
        private static final long serialVersionUID = 1L;
        protected static final JsonInclude.Value EMPTY;
        protected final JsonInclude.Include _valueInclusion;
        protected final JsonInclude.Include _contentInclusion;
        protected final Class<?> _valueFilter;
        protected final Class<?> _contentFilter;

        public Value(JsonInclude src) {
            this(src.value(), src.content(), src.valueFilter(), src.contentFilter());
        }

        protected Value(JsonInclude.Include vi, JsonInclude.Include ci, Class<?> valueFilter, Class<?> contentFilter) {
            this._valueInclusion = vi == null ? JsonInclude.Include.USE_DEFAULTS : vi;
            this._contentInclusion = ci == null ? JsonInclude.Include.USE_DEFAULTS : ci;
            this._valueFilter = valueFilter == Void.class ? null : valueFilter;
            this._contentFilter = contentFilter == Void.class ? null : contentFilter;
        }

        public static JsonInclude.Value empty() {
            return EMPTY;
        }

        public static JsonInclude.Value merge(JsonInclude.Value base, JsonInclude.Value overrides) {
            return base == null ? overrides : base.withOverrides(overrides);
        }

        public static JsonInclude.Value mergeAll(JsonInclude.Value... values) {
            JsonInclude.Value result = null;
            JsonInclude.Value[] var2 = values;
            int var3 = values.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                JsonInclude.Value curr = var2[var4];
                if (curr != null) {
                    result = result == null ? curr : result.withOverrides(curr);
                }
            }

            return result;
        }

        protected Object readResolve() {
            return this._valueInclusion == JsonInclude.Include.USE_DEFAULTS && this._contentInclusion == JsonInclude.Include.USE_DEFAULTS && this._valueFilter == null && this._contentFilter == null ? EMPTY : this;
        }

        public JsonInclude.Value withOverrides(JsonInclude.Value overrides) {
            if (overrides != null && overrides != EMPTY) {
                JsonInclude.Include vi = overrides._valueInclusion;
                JsonInclude.Include ci = overrides._contentInclusion;
                Class<?> vf = overrides._valueFilter;
                Class<?> cf = overrides._contentFilter;
                boolean viDiff = vi != this._valueInclusion && vi != JsonInclude.Include.USE_DEFAULTS;
                boolean ciDiff = ci != this._contentInclusion && ci != JsonInclude.Include.USE_DEFAULTS;
                boolean filterDiff = vf != this._valueFilter || cf != this._valueFilter;
                if (viDiff) {
                    return ciDiff ? new JsonInclude.Value(vi, ci, vf, cf) : new JsonInclude.Value(vi, this._contentInclusion, vf, cf);
                } else if (ciDiff) {
                    return new JsonInclude.Value(this._valueInclusion, ci, vf, cf);
                } else {
                    return filterDiff ? new JsonInclude.Value(this._valueInclusion, this._contentInclusion, vf, cf) : this;
                }
            } else {
                return this;
            }
        }

        public static JsonInclude.Value construct(JsonInclude.Include valueIncl, JsonInclude.Include contentIncl) {
            return valueIncl != JsonInclude.Include.USE_DEFAULTS && valueIncl != null || contentIncl != JsonInclude.Include.USE_DEFAULTS && contentIncl != null ? new JsonInclude.Value(valueIncl, contentIncl, (Class)null, (Class)null) : EMPTY;
        }

        public static JsonInclude.Value construct(JsonInclude.Include valueIncl, JsonInclude.Include contentIncl, Class<?> valueFilter, Class<?> contentFilter) {
            if (valueFilter == Void.class) {
                valueFilter = null;
            }

            if (contentFilter == Void.class) {
                contentFilter = null;
            }

            return (valueIncl == JsonInclude.Include.USE_DEFAULTS || valueIncl == null) && (contentIncl == JsonInclude.Include.USE_DEFAULTS || contentIncl == null) && valueFilter == null && contentFilter == null ? EMPTY : new JsonInclude.Value(valueIncl, contentIncl, valueFilter, contentFilter);
        }

        public static JsonInclude.Value from(JsonInclude src) {
            if (src == null) {
                return EMPTY;
            } else {
                JsonInclude.Include vi = src.value();
                JsonInclude.Include ci = src.content();
                if (vi == JsonInclude.Include.USE_DEFAULTS && ci == JsonInclude.Include.USE_DEFAULTS) {
                    return EMPTY;
                } else {
                    Class<?> vf = src.valueFilter();
                    if (vf == Void.class) {
                        vf = null;
                    }

                    Class<?> cf = src.contentFilter();
                    if (cf == Void.class) {
                        cf = null;
                    }

                    return new JsonInclude.Value(vi, ci, vf, cf);
                }
            }
        }

        public JsonInclude.Value withValueInclusion(JsonInclude.Include incl) {
            return incl == this._valueInclusion ? this : new JsonInclude.Value(incl, this._contentInclusion, this._valueFilter, this._contentFilter);
        }

        public JsonInclude.Value withValueFilter(Class<?> filter) {
            JsonInclude.Include incl;
            if (filter != null && filter != Void.class) {
                incl = JsonInclude.Include.CUSTOM;
            } else {
                incl = JsonInclude.Include.USE_DEFAULTS;
                filter = null;
            }

            return construct(incl, this._contentInclusion, filter, this._contentFilter);
        }

        public JsonInclude.Value withContentFilter(Class<?> filter) {
            JsonInclude.Include incl;
            if (filter != null && filter != Void.class) {
                incl = JsonInclude.Include.CUSTOM;
            } else {
                incl = JsonInclude.Include.USE_DEFAULTS;
                filter = null;
            }

            return construct(this._valueInclusion, incl, this._valueFilter, filter);
        }

        public JsonInclude.Value withContentInclusion(JsonInclude.Include incl) {
            return incl == this._contentInclusion ? this : new JsonInclude.Value(this._valueInclusion, incl, this._valueFilter, this._contentFilter);
        }

        public Class<JsonInclude> valueFor() {
            return JsonInclude.class;
        }

        public JsonInclude.Include getValueInclusion() {
            return this._valueInclusion;
        }

        public JsonInclude.Include getContentInclusion() {
            return this._contentInclusion;
        }

        public Class<?> getValueFilter() {
            return this._valueFilter;
        }

        public Class<?> getContentFilter() {
            return this._contentFilter;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder(80);
            sb.append("JsonInclude.Value(value=").append(this._valueInclusion).append(",content=").append(this._contentInclusion);
            if (this._valueFilter != null) {
                sb.append(",valueFilter=").append(this._valueFilter.getName()).append(".class");
            }

            if (this._contentFilter != null) {
                sb.append(",contentFilter=").append(this._contentFilter.getName()).append(".class");
            }

            return sb.append(')').toString();
        }

        public int hashCode() {
            return (this._valueInclusion.hashCode() << 2) + this._contentInclusion.hashCode();
        }

        public boolean equals(Object o) {
            if (o == this) {
                return true;
            } else if (o == null) {
                return false;
            } else if (o.getClass() != this.getClass()) {
                return false;
            } else {
                JsonInclude.Value other = (JsonInclude.Value)o;
                return other._valueInclusion == this._valueInclusion && other._contentInclusion == this._contentInclusion && other._valueFilter == this._valueFilter && other._contentFilter == this._contentFilter;
            }
        }

        static {
            EMPTY = new JsonInclude.Value(JsonInclude.Include.USE_DEFAULTS, JsonInclude.Include.USE_DEFAULTS, (Class)null, (Class)null);
        }
    }

    public static enum Include {
        ALWAYS,
        NON_NULL,
        NON_ABSENT,
        NON_EMPTY,
        NON_DEFAULT,
        CUSTOM,
        USE_DEFAULTS;

        private Include() {
        }
    }
}