如何隐藏“children”:[]在springboot的一对多关系中的最后一个子元素

jjjwad0x  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(357)

实际上,我想对我的react本机应用程序使用treejson响应。所以我把它放在Spring Boot里。但由于“孩子”:[]在最后一个孩子,我面临一些问题,在React Native。所以我想在我的回答中隐藏这一点。
我得到了这种回应

[{
            "id": 1,
            "name": "Martin",                
            "haveAccount": false,
            "gender": "m",
            "children": [
                {
                    "id": 4,
                    "name": "Werite",
                    "haveAccount": false,
                    "gender": "f",
                    "children": []
                }

            ]
        }]

但我不想要“孩子”:[]在最后一个孩子
example:-

[{
                "id": 1,
                "name": "Martin",                
                "haveAccount": false,
                "gender": "m",
                "children": [
                    {
                        "id": 4,
                        "name": "Werite",
                        "haveAccount": false,
                        "gender": "f"
                    }

                ]
            }]

这是我的实体类

@Service
@Entity
@Table
public class Family {   
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
@Column(name="Family_id")
private Integer id;
private String name;
private String village;
private String wifeName;
private String talukaName;
private Boolean haveAccount;
private String username;
private String gender;
private String mobileNumber;

@OneToOne
@JoinColumn(name = "parent_id")
@JsonBackReference

private Family parent;

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
@JsonManagedReference
private List<Family> children = new ArrayList<>();

public Family(Integer id, String name, String village, String wifeName, String talukaName, Boolean haveAccount,
        String username, String gender, String mobileNumber, Family parent, List<Family> children) {
    super();
    this.id = id;
    this.name = name;
    this.village = village;
    this.wifeName = wifeName;
    this.talukaName = talukaName;
    this.haveAccount = haveAccount;
    this.username = username;
    this.gender = gender;
    this.mobileNumber = mobileNumber;
    this.parent = parent;
    this.children = children;
}
public Family() {
super();

}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

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

public Family getParent() {
    return parent;
}

public void setParent(Family parent) {
    this.parent = parent;
}

public List<Family> getChildren() {
    return children;
}

public void setChildren(List<Family> children) {
    this.children = children;
}

public void addChild(Family children) {
    this.children.add(children);
}

public String getVillage() {
    return village;
}

public void setVillage(String village) {
    this.village = village;
}

public String getWifeName() {
    return wifeName;
}

public void setWifeName(String wifeName) {
    this.wifeName = wifeName;
}

public String getTalukaName() {
    return talukaName;
}

public void setTalukaName(String talukaName) {
    this.talukaName = talukaName;
}

public Boolean getHaveAccount() {
    return haveAccount;
}

public void setHaveAccount(Boolean haveAccount) {
    this.haveAccount = haveAccount;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getMobileNumber() {
    return mobileNumber;
}

public void setMobileNumber(String mobileNumber) {
    this.mobileNumber = mobileNumber;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

}

nbysray5

nbysray51#

如果你想删除一个字段,你可以把它设为null并使用 @JsonInclude(Include.NON_NULL) 在你的课堂上如下:

@Service
@Entity
@Table
@JsonInclude(Include.NON_NULL)
public class Family {  
  //class
}

如果你能处理好,你应该用 private List<Family> children = new ArrayList<>();private List<Family> children; 当你要使用它的时候初始化它。
只是一个问题:为什么在实体类上使用@service?

相关问题