如何隐藏 "children": [] from last child in one to many relationship in spring boot
How to hide "children": [] from last child in one to many relationship in spring boot
实际上我想为我的 React Native 应用程序使用树 Json 响应。所以我在 spring 启动时构建它。但是由于 "children": [] in last child 我在 React Native 中遇到了一些问题。所以我想在我的回复中隐藏它。
1.I 收到了此类回复
[{
"id": 1,
"name": "Martin",
"haveAccount": false,
"gender": "m",
"children": [
{
"id": 4,
"name": "Werite",
"haveAccount": false,
"gender": "f",
"children": []
}
]
}]
2.But 我不要 "children": [] 最后 child
示例:-
[{
"id": 1,
"name": "Martin",
"haveAccount": false,
"gender": "m",
"children": [
{
"id": 4,
"name": "Werite",
"haveAccount": false,
"gender": "f"
}
]
}]
3.Here 是我的实体 class
@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;
}
}
如果您想删除一个字段,您可以将其设为空,然后在您的 class 上使用 @JsonInclude(Include.NON_NULL)
,如下所示:
@Service
@Entity
@Table
@JsonInclude(Include.NON_NULL)
public class Family {
//class
}
如果你能处理好,你应该用 private List<Family> children;
替换 private List<Family> children = new ArrayList<>();
并在你要使用它时初始化它。
只是一个问题:为什么要在您的实体上使用 @Service class?
实际上我想为我的 React Native 应用程序使用树 Json 响应。所以我在 spring 启动时构建它。但是由于 "children": [] in last child 我在 React Native 中遇到了一些问题。所以我想在我的回复中隐藏它。
1.I 收到了此类回复
[{
"id": 1,
"name": "Martin",
"haveAccount": false,
"gender": "m",
"children": [
{
"id": 4,
"name": "Werite",
"haveAccount": false,
"gender": "f",
"children": []
}
]
}]
2.But 我不要 "children": [] 最后 child
示例:-
[{
"id": 1,
"name": "Martin",
"haveAccount": false,
"gender": "m",
"children": [
{
"id": 4,
"name": "Werite",
"haveAccount": false,
"gender": "f"
}
]
}]
3.Here 是我的实体 class
@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;
}
}
如果您想删除一个字段,您可以将其设为空,然后在您的 class 上使用 @JsonInclude(Include.NON_NULL)
,如下所示:
@Service
@Entity
@Table
@JsonInclude(Include.NON_NULL)
public class Family {
//class
}
如果你能处理好,你应该用 private List<Family> children;
替换 private List<Family> children = new ArrayList<>();
并在你要使用它时初始化它。
只是一个问题:为什么要在您的实体上使用 @Service class?