java - 如何使用 @JsonIgnoreProperties 忽略 JSON 响应上的特定子属性?

标签 java json spring-boot hibernate jpa

我有 2 个名为 masterclient 和 userexternal 的模型类

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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8aebebebebcaeba4e9e5e7" rel="noreferrer noopener nofollow">[email protected]</a>",
    "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"但它不起作用。 任何帮助将不胜感激,谢谢。

最佳答案

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

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

关于java - 如何使用 @JsonIgnoreProperties 忽略 JSON 响应上的特定子属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62448107/

相关文章:

css - Sass:如何将 JSON 文件直接解码为 Sass 变量?

php - jQuery/JSON/PHP 失败

javascript - Eureka Server 在 Spring Boot 启动时出现错误

java - 如何解决错误 : Could not find or load main class in SpringBoot application

java - Spring 奇怪地取决于上课地点

java - 确定我的应用程序从未安装在设备上以及用户是否正在重新安装的最佳方法是什么?

java - maven本地缓存错误

java - servlet 中的 xpath 查询给出异常

JSON 将整数字段解码为字符串

Java等到线程(Threadpool)完成他们的工作并开始另一个工作