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() {
}
}
}
相關文章
- java透過反射統計實體類和父類中為空的欄位數量Java反射
- 如何過濾掉 PHP 陣列中的空值?PHP陣列
- python+resuests 傳送請求,結果中如果欄位的值是 null 就會被 json 自動過濾掉,請問如何才能不被過濾PythonNullJSON
- JSP中String a = request.getParameter(“ “),判斷a是否為null或空的問題JSNull
- MySQL-去掉不為null的欄位MySqlNull
- Spring Cloud Gateway過濾器精確控制異常返回(實戰,控制http返回碼和message欄位)SpringCloudGateway過濾器HTTP
- springMVC中controller的返回值SpringMVCController
- laravel-query-builder 對於欄位 值為 null的排序方式LaravelUINull排序
- sqlsugar 實現實體類中欄位是字串陣列情況SqlSugar字串陣列
- Mybatis-Plus 更新欄位為 NULLMyBatisNull
- SpringBoot中的過濾器和攔截器的實現Spring Boot過濾器
- 通過java反射,遞迴查詢物件所有屬性,為空也要返回欄位的結構Java反射遞迴物件
- Controller內注入的Service為nullControllerNull
- SpringBoot圖文教程6—SpringBoot中過濾器的使用Spring Boot過濾器
- Jquery 替換掉路徑中的某些欄位 replaceAll(selector)jQuery
- .NET Core 處理 WebAPI JSON 返回煩人的null為空WebAPIJSONNull
- 類中的結構體或列舉等型別的前置宣告結構體型別
- 請問如何把檔案中的空行過濾掉(要求命令列實現)命令列
- java動態獲取實體類的欄位Java
- mysql中null與“空值”的坑MySqlNull
- Java判斷欄位是否為空,為空賦值 ?Java賦值
- springboot請求controller返回Whitelabel Error PageSpring BootControllerError
- java中判斷String型別為空和null的方法Java型別Null
- [20181031]lob欄位與布隆過濾.txt
- Spring 攔截器和過濾器中自動注入為 null 的原因及解決方案Spring過濾器Null
- 為什麼資料庫欄位要使用NOT NULL?資料庫Null
- 技術乾貨| MongoDB如何查詢Null或不存在的欄位?MongoDBNull
- 實現註解校驗Dto欄位是否為空
- 在 React 16 中從 setState 返回 null 的妙用ReactNull
- springboot redisTemplate.opsForValue().setIfAbsent返回null原理Spring BootRedisNull
- EF更新指定欄位.或個更新整個實體
- SpringBoot2.1版本的個人應用開發框架 - 實現基礎controller、service、前端封裝返回json體等Spring Boot框架Controller前端封裝JSON
- SpringBoot在controller返回一個HTML頁面Spring BootControllerHTML
- json轉化保留null欄位JSONNull
- 如何獲取java類中的欄位修飾符?Java
- java統計實體類中空欄位數量Java
- 為 type 等欄位「保駕護航」
- JPA實體中欄位對映補充和嵌入物件物件