国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

java后端返回數(shù)據(jù)給前端時去除值為空或NULL的屬性、忽略某些屬性

這篇具有很好參考價值的文章主要介紹了java后端返回數(shù)據(jù)給前端時去除值為空或NULL的屬性、忽略某些屬性。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

目錄

一、使用場景

二、環(huán)境準備

1、引入依賴

2、實體類

三、示例

1、不返回空值

(1)方式

(2)測試

(3)說明

2、不返回部分屬性

(1)方式

(2)測試

四、?Jackson常用注解

1、 @JsonProperty

2、@JsonPropertyOrder

3、@JsonInclude

4、@JsonIgnoreProperties

5、@JsonFormat

6、@JsonUnwrapped


一、使用場景

????????在開發(fā)過程中,有時候需要將后端數(shù)據(jù)返回前端,此時有些數(shù)據(jù)為空屬性不需要返回,或者有些屬性不需要返回,因此就需要處理。

返回值忽略null,Java,java,前端,開發(fā)語言,后端

二、環(huá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、實體類


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)方式

在實體類上面加上下面的注解:

@JsonInclude(JsonInclude.Include.NON_EMPTY)

?返回值忽略null,Java,java,前端,開發(fā)語言,后端

(2)測試

Controller里面的方法:

    @PostMapping("/getData")
    public R getData(){
        Student student = new Student();
        student.setName("Tom");
        student.setAge(22);
        return R.ok().data("student", student);
    }

測試結(jié)果:

返回值忽略null,Java,java,前端,開發(fā)語言,后端

(3)說明

如果要對部分屬性進行空值限制,分為兩類:

  • 字符串、基本數(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)方式

實體類屬性上使用注解:

@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é)果:

返回值忽略null,Java,java,前端,開發(fā)語言,后端

四、?Jackson常用注解

1、 @JsonProperty

????????此注解用于屬性上,作用是把該屬性的名稱序列化為另外一個名稱,如把testPwd屬性序列化為pwd,@JsonProperty(value="pwd")。

2、@JsonPropertyOrder

????????作用在類上,被用來指明當序列化時需要對屬性做排序,它有2個屬性一個是alphabetic:布爾類型,表示是否采用字母拼音順序排序,默認是為false,即不排序。如@JsonPropertyOrder(alphabetic=true)。

3、@JsonInclude

????????是用在實體類的方法類的頭上 作用是實體類的參數(shù)查詢到的為null的不顯示,比如說你想傳一些json數(shù)據(jù)到前臺,但是不想傳值為null的數(shù)據(jù),就可以使用該標簽。如@JsonInclude(JsonInclude.Include.NON_NULL)

4、@JsonIgnoreProperties

????????可以注明是想要忽略的屬性列表如@JsonIgnoreProperties({"name","age","title"}),也可以注明過濾掉未知的屬性如@JsonIgnoreProperties(ignoreUnknown=true),@JsonIgnore表示忽略當前屬性。

5、@JsonFormat

????????用在屬性和方法上,可以方便的進行格式轉(zhuǎn)換,如把Date轉(zhuǎn)換為我們要的模式@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。

6、@JsonUnwrapped

????????當實體類中成員屬性是一個類的對象時候,忽略包裝。直接顯示屬性。文章來源地址http://www.zghlxwxcb.cn/news/detail-764523.html

到了這里,關(guān)于java后端返回數(shù)據(jù)給前端時去除值為空或NULL的屬性、忽略某些屬性的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包