springboot中controller返回實體類中過濾掉等於null或為空的欄位
使用springboot中註解:
在需要過濾的欄位上或實體類上面新增:
//列舉值:ALWAYS,NON_NULL,NON_ABSENT,NON_EMPTY,NON_DEFAULT,CUSTOM,USE_DEFAULTS
@JsonInclude(Include.NON_EMPTY)
其它引數:
-
//將該標記放在屬性上,如果該屬性為NULL則不參與序列化
-
//如果放在類上邊,那對這個類的全部屬性起作用
-
//Include.ALWAYS 預設
-
//Include.NON_DEFAULT 屬性為預設值不序列化
-
//Include.NON_EMPTY 屬性為空(“”)或者為 NULL 都不序列化
-
//Include.NON_NULL 屬性為NULL 不序列化
例如:當該類中的屬性為null或為空時,將以json返回物件序列化時這些屬性不會序列化。
@JsonInclude(Include.NON_EMPTY)
public class GetLabelDataVO {
private String labelNumber;
private List<TaskWorkingRecord> taskWorkingRecords;
private List<CountAnswer> countAnswers;
public String getLabelNumber() {
return labelNumber;
}
public void setLabelNumber(String labelNumber) {
this.labelNumber = labelNumber;
}
public List<TaskWorkingRecord> getTaskWorkingRecords() {
return taskWorkingRecords;
}
public void setTaskWorkingRecords(
List<TaskWorkingRecord> taskWorkingRecords) {
this.taskWorkingRecords = taskWorkingRecords;
}
public List<CountAnswer> getCountAnswers() {
return countAnswers;
}
public void setCountAnswers(List<CountAnswer> countAnswers) {
this.countAnswers = countAnswers;
}
}
原始碼:
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[] arr$ = values;
int len$ = values.length;
for(int i$ = 0; i$ < len$; ++i$) {
JsonInclude.Value curr = arr$[i$];
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() {
}
}
}
相關文章
- 如何過濾掉 PHP 陣列中的空值?PHP陣列
- python+resuests 傳送請求,結果中如果欄位的值是 null 就會被 json 自動過濾掉,請問如何才能不被過濾PythonNullJSON
- 表中已有資料,將表中某個欄位為空的改為非空
- MySQL中NULL欄位的比較問題MySqlNull
- sqlsugar 實現實體類中欄位是字串陣列情況SqlSugar字串陣列
- mysql 查詢欄位為null或者非nullMySqlNull
- ASP.NET Web API中通過URI顯示實體中的部分欄位ASP.NETWebAPI
- MySQL-去掉不為null的欄位MySqlNull
- laravel-query-builder 對於欄位 值為 null的排序方式LaravelUINull排序
- Mybatis-Plus 更新欄位為 NULLMyBatisNull
- 對錶中的欄位設定了預設值,新增記錄後卻發現該欄位為nullNull
- SQL中的空值NULLSQLNull
- Linq強大的查詢功能,以及DataSet中多表之間交叉查詢,欄位過濾,篩選等
- JSP中String a = request.getParameter(“ “),判斷a是否為null或空的問題JSNull
- 通過java反射,遞迴查詢物件所有屬性,為空也要返回欄位的結構Java反射遞迴物件
- SpringBoot中的過濾器和攔截器的實現Spring Boot過濾器
- Spring Cloud Gateway過濾器精確控制異常返回(實戰,控制http返回碼和message欄位)SpringCloudGateway過濾器HTTP
- 關於ResultFilter類中NULL_INT的作用????FilterNull
- 類中的結構體或列舉等型別的前置宣告結構體型別
- 請問如何把檔案中的空行過濾掉(要求命令列實現)命令列
- Jquery 替換掉路徑中的某些欄位 replaceAll(selector)jQuery
- springMVC中controller的返回值SpringMVCController
- 資料庫索引欄位請不要為NULL資料庫索引Null
- java動態獲取實體類的欄位Java
- Java判斷欄位是否為空,為空賦值 ?Java賦值
- mysql中null與“空值”的坑MySqlNull
- .net中通過反射得到所有的私有欄位(包括父類)反射
- 對資料庫中的表或欄位重新命名資料庫
- SpringBoot圖文教程6—SpringBoot中過濾器的使用Spring Boot過濾器
- .NET Core 處理 WebAPI JSON 返回煩人的null為空WebAPIJSONNull
- 在 React 16 中從 setState 返回 null 的妙用ReactNull
- 實現註解校驗Dto欄位是否為空
- Controller內注入的Service為nullControllerNull
- 如何獲取java類中的欄位修飾符?Java
- 為什麼資料庫欄位要使用NOT NULL?資料庫Null
- Oracle中的Rownum 欄位Oracle
- Oracle中的大欄位Oracle
- Spring 攔截器和過濾器中自動注入為 null 的原因及解決方案Spring過濾器Null