java后端返回?cái)?shù)據(jù)給前端時(shí)去除值為空或NULL的屬性、忽略某些屬性代碼示例
一、使用場景
在開發(fā)過程中,有時(shí)候需要將后端數(shù)據(jù)返回前端,此時(shí)有些數(shù)據(jù)為空屬性不需要返回,或者有些屬性不需要返回,因此就需要處理。

二、環(huán)境準(zhǔn)備
1、引入依賴
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
2、實(shí)體類
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student{
private Integer id;
private String name;
private Integer age;
private String address;
private BigDecimal score;
private String className;
private List<String> subjectList = new ArrayList<>();
}
三、示例
1、不返回空值
(1)方式
在實(shí)體類上面加上下面的注解:
@JsonInclude(JsonInclude.Include.NON_EMPTY)

(2)測試
Controller里面的方法:
@PostMapping("/getData")
public R getData(){
Student student = new Student();
student.setName("Tom");
student.setAge(22);
return R.ok().data("student", student);
}測試結(jié)果:

(3)說明
如果要對部分屬性進(jìn)行空值限制,分為兩類:
- 字符串、基本數(shù)據(jù)類型的設(shè)置,使用JsonInclude.Include.NON_NULL
- 對象、數(shù)組之類的設(shè)置,使用JsonInclude.Include.NON_EMPTY
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student{
@JsonInclude(JsonInclude.Include.NON_NULL)
private Integer id;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String name;
@JsonInclude(JsonInclude.Include.NON_NULL)
private Integer age;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String address;
@JsonInclude(JsonInclude.Include.NON_NULL)
private BigDecimal score;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String className;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<String> subjectList = new ArrayList<>();
}2、不返回部分屬性
(1)方式
實(shí)體類屬性上使用注解:
@JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Student {
private Integer id;
private String name;
private Integer age;
@JsonIgnore
private String address;
private BigDecimal score;
private String className;
private List<String> subjectList = new ArrayList<>();
}
(2)測試
Controller里面的方法:
@PostMapping("/getData")
public R getData(){
Student student = new Student();
student.setId(1001);
student.setName("Tom");
student.setAge(22);
student.setAddress("浙江");
return R.ok().data("student", student);
}測試結(jié)果:

四、 Jackson常用注解
1、 @JsonProperty
此注解用于屬性上,作用是把該屬性的名稱序列化為另外一個(gè)名稱,如把testPwd屬性序列化為pwd,@JsonProperty(value="pwd")。
2、@JsonPropertyOrder
作用在類上,被用來指明當(dāng)序列化時(shí)需要對屬性做排序,它有2個(gè)屬性一個(gè)是alphabetic:布爾類型,表示是否采用字母拼音順序排序,默認(rèn)是為false,即不排序。如@JsonPropertyOrder(alphabetic=true)。
3、@JsonInclude
是用在實(shí)體類的方法類的頭上 作用是實(shí)體類的參數(shù)查詢到的為null的不顯示,比如說你想傳一些json數(shù)據(jù)到前臺,但是不想傳值為null的數(shù)據(jù),就可以使用該標(biāo)簽。如@JsonInclude(JsonInclude.Include.NON_NULL)
4、@JsonIgnoreProperties
可以注明是想要忽略的屬性列表如@JsonIgnoreProperties({"name","age","title"}),也可以注明過濾掉未知的屬性如@JsonIgnoreProperties(ignoreUnknown=true),@JsonIgnore表示忽略當(dāng)前屬性。
5、@JsonFormat
用在屬性和方法上,可以方便的進(jìn)行格式轉(zhuǎn)換,如把Date轉(zhuǎn)換為我們要的模式@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。
6、@JsonUnwrapped
當(dāng)實(shí)體類中成員屬性是一個(gè)類的對象時(shí)候,忽略包裝。直接顯示屬性。
總結(jié)
到此這篇關(guān)于java后端返回?cái)?shù)據(jù)給前端時(shí)去除值為空或NULL的屬性、忽略某些屬性的文章就介紹到這了,更多相關(guān)java后端返回?cái)?shù)據(jù)去除空值或NULL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中Collections.sort排序函數(shù)用法詳解
本篇文章主要介紹了java中Collections.sort排序函數(shù)用法詳解,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12
springBoot項(xiàng)目如何實(shí)現(xiàn)啟動多個(gè)實(shí)例
這篇文章主要介紹了springBoot項(xiàng)目如何實(shí)現(xiàn)啟動多個(gè)實(shí)例的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
如何使用Spring Security手動驗(yàn)證用戶的方法示例
這篇文章主要介紹了如何使用Spring Security手動驗(yàn)證用戶的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
MyBatisPlus 暫時(shí)分頁失效原因及問題解決
MybatisPlus升級后分頁失效,因新版本PaginationInnerInterceptor中新增maxLimit限制,下面就來介紹MyBatisPlus 暫時(shí)分頁失效原因及問題解決,感興趣的可以了解一下2025-07-07
Springboot熱加載JAR包的實(shí)現(xiàn)方法
SpringBoot作為一個(gè)開發(fā)快速、部署方便的微服務(wù)框架,具有自動配置、約定優(yōu)于配置的特點(diǎn),能夠極大地提高開發(fā)效率,它提供了豐富的擴(kuò)展點(diǎn),非常適合實(shí)現(xiàn)動態(tài)加載Jar包的功能,本文將深入探討如何在SpringBoot應(yīng)用中實(shí)現(xiàn)動態(tài)加載Jar包的方案,感興趣的朋友一起看看吧2024-04-04
Mybatis工具類JdbcTypeInterceptor運(yùn)行時(shí)自動添加jdbcType屬性
今天小編就為大家分享一篇關(guān)于Mybatis工具類JdbcTypeInterceptor運(yùn)行時(shí)自動添加jdbcType屬性,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

