簡體   English   中英

如何使用@JsonIgnoreProperties 忽略 JSON 響應上的特殊子屬性?

[英]How to ignore spesific child properties on JSON response using @JsonIgnoreProperties?

我有 2 個名為 masterclient 和 userexternal 的 model 類

import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


@Entity
@Table(name = "user_external")
public class UserExternalModel extends AuditModel {
    @Id
    @GeneratedValue(generator = "user_external_generator")
    @SequenceGenerator(
            name = "user_external_generator",
            sequenceName = "user_external_sequence",
            initialValue = 1
    )
    @Column(name = "user_id", nullable = false)
    private long userId;

    @NotBlank
    @Size(min = 1, max = 65)
    @Column(name = "name", nullable = false)
    private String name;

    @NotBlank
    @javax.validation.constraints.Email
    @Size(min = 1, max = 65)
    @Column(name = "email", nullable = false)
    private String email;

    @Column(name = "active")
    private int active;

    @Column(name = "inactive_by", nullable = true)
    private int inactiveBy;

    @Column(name = "phone_number", nullable = true)
    @Size(min = 1, max = 15)
    private String phoneNumber;

    @Column(name = "password", nullable = true)
    @Size(min = 1, max = 65)
    private String password;


    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @MapsId("client_id")
    @JoinColumn(name = "clientId", referencedColumnName = "client_id")
    private MasterClientModel masterClientModel;
    private long clientId;

    @OneToMany(mappedBy = "userExtModel", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    Set<EngagementUserExtModel> engUserExts = new HashSet<>();

    public UserExternalModel() {
    }


    // Getters and Setters (Omitted for brevity)
    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getActive() {
        return active;
    }

    public void setActive(int active) {
        this.active = active;
    }

    public int getInactiveBy() {
        return inactiveBy;
    }

    public void setInactiveBy(int inactiveBy) {
        this.inactiveBy = inactiveBy;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public MasterClientModel getMasterClientModel() {
        return masterClientModel;
    }

    public void setMasterClientModel(MasterClientModel masterClientModel) {
        this.masterClientModel = masterClientModel;
    }


    public long getClientId() {
        return clientId;
    }


    public void setClientId(long clientId) {
        this.clientId = clientId;
    }

}

然后

import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.*;
    import javax.validation.constraints.Size;
    import org.hibernate.annotations.Type;

    @Entity
    @Table(name = "master_client")
    public class MasterClientModel extends AuditModel {
        @Id
        @GeneratedValue(generator = "master_client_generator")
        @SequenceGenerator(
                name = "master_client_generator",
                sequenceName = "master_client_sequence",
                initialValue = 1
        )
        @Column(name = "client_id", nullable = false)
        private long clientId;

        @Size(min = 1, max = 15)
        @Column(name = "npwp", nullable = false)
        private String npwp;

        @Size(min = 1, max = 65)
        @Column(name = "company_name", nullable = false)
        private String companyName;

        @Lob
        @Type(type="org.hibernate.type.BinaryType")
        @Column(name = "logo", updatable = true, columnDefinition="image")   
        private byte[] logo;

        @Column(name = "description", nullable = true)
        @Size(max = 255)
        private String description;    


        @OneToMany(mappedBy = "masterClientModel", fetch = FetchType.LAZY)
        Set<UserExternalModel> userExts = new HashSet<>();

        @OneToMany(mappedBy = "masterClientModel", fetch = FetchType.LAZY)
        Set<EngagementModel> engagements = new HashSet<>();


        // Getters and Setters (Omitted for brevity)
        public long getClientId() {
            return clientId;
        }

        public void setClientId(long clientId) {
            this.clientId = clientId;
        }

        public String getNpwp() {
            return npwp;
        }

        public void setNpwp(String npwp) {
            this.npwp = npwp;
        }

        public String getCompanyName() {
            return companyName;
        }

        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }

        public byte[] getLogo() {
            return logo;
        }

        public void setLogo(byte[] logo) {
            this.logo = logo;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

    }  

然后當我在 userexternal 上調用我的 API 請求時,給出的響應如下:

{
    "createdDate": "2020-06-18T08:24:17.263+00:00",
    "updatedDate": "2020-06-18T08:24:17.263+00:00",
    "userId": 1,
    "name": "ZZZZZZZZZZZ",
    "email": "aaaa@a.com",
    "active": 1,
    "inactiveBy": 2,
    "phoneNumber": "123123123",
    "password": "dualipa",
    "masterClientModel": {
        "createdDate": "2020-06-16T07:33:35.996+00:00",
        "updatedDate": "2020-06-16T07:33:35.996+00:00",
        "clientId": 1,
        "npwp": "12312312312321",
        "companyName": "PT A",
        "description": "A",
        "hibernateLazyInitializer": {}
    },
    "clientId": 1
}

我的問題是,當我點擊 userexternal API 請求時,如何刪除“masterClientModel”下的一些屬性,例如 npwp、公司名稱等,但在從 masterclientmodel 中點擊 API 請求時使其保持可見/包含。 我試過 jsonignoreproperties value="npwp" 但它沒有用。 任何幫助將不勝感激,謝謝。

在要從響應中排除的屬性上使用@JsonIgnore注釋

例子:

@Entity
@Table(name = "master_client")
public class MasterClientModel extends AuditModel {
    @Id
    @GeneratedValue(generator = "master_client_generator")
    @SequenceGenerator(
            name = "master_client_generator",
            sequenceName = "master_client_sequence",
            initialValue = 1
    )
    @Column(name = "client_id", nullable = false)
    private long clientId;

    @Size(min = 1, max = 15)
    @Column(name = "npwp", nullable = false)
    @JsonIgnore 
    private String npwp;

    @Size(min = 1, max = 65)
    @Column(name = "company_name", nullable = false)
    private String companyName;

    @Lob
    @Type(type="org.hibernate.type.BinaryType")
    @Column(name = "logo", updatable = true, columnDefinition="image")   
    private byte[] logo;

    @Column(name = "description", nullable = true)
    @Size(max = 255)
    private String description;    


    @OneToMany(mappedBy = "masterClientModel", fetch = FetchType.LAZY)
    Set<UserExternalModel> userExts = new HashSet<>();

    @OneToMany(mappedBy = "masterClientModel", fetch = FetchType.LAZY)
    Set<EngagementModel> engagements = new HashSet<>();


    // Getters and Setters (Omitted for brevity)
    public long getClientId() {
        return clientId;
    }

    public void setClientId(long clientId) {
        this.clientId = clientId;
    }

    public String getNpwp() {
        return npwp;
    }

    public void setNpwp(String npwp) {
        this.npwp = npwp;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public byte[] getLogo() {
        return logo;
    }

    public void setLogo(byte[] logo) {
        this.logo = logo;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

} 

在字段上添加@JsonIgnore

@Size(min = 1, max = 15)
@Column(name = "npwp", nullable = false)
@JsonIgnore
private String npwp;

您可以在子字段上使用@JsonIgnoreProperties來忽略其屬性

public class UserExternalModel extends AuditModel {
   ...
   @JsonIgnoreProperties({"npwp", "companyName"})
   private MasterClientModel masterClientModel;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM