1
votes

Comment ignorer des propriétés enfants spécifiques sur une réponse JSON à l'aide de @JsonIgnoreProperties?

J'ai 2 classes de modèles nommées masterclient et userexternal

{
    "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
}

puis

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;
        }

    }  

puis quand j'ai appelé ma requête API sur userexternal, étant donné réponse comme ci-dessous:

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;
    }

}

Ma question est de savoir comment je peux supprimer certaines propriétés sous "masterClientModel" telles que npwp, nom de société, etc. inclus lorsque vous frappez la demande d'API de masterclientmodel. J'ai essayé avec jsonignoreproperties value = "npwp" mais cela n'a pas fonctionné. toute aide sera appréciée, merci.


0 commentaires

3 Réponses :


0
votes

utilisez l'annotation @JsonIgnore sur la propriété que vous souhaitez exclure de la réponse

exemple:

@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;
    }

} 


0 commentaires

0
votes

Ajoutez @JsonIgnore dans le champ

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


0 commentaires

0
votes

Vous pouvez utiliser @JsonIgnoreProperties sur le champ enfant pour ignorer ses propriétés

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


0 commentaires