Actually I want to use tree Json response for my react native application. So I build that in spring boot. But due to "children": [] in last child I face some issue in react native. So I want to hide that from my response.
1.I got this type of response
[{
"id": 1,
"name": "Martin",
"haveAccount": false,
"gender": "m",
"children": [
{
"id": 4,
"name": "Werite",
"haveAccount": false,
"gender": "f",
"children": []
}
]
}]
2.But I don't want "children": [] in last child
example:-
[{
"id": 1,
"name": "Martin",
"haveAccount": false,
"gender": "m",
"children": [
{
"id": 4,
"name": "Werite",
"haveAccount": false,
"gender": "f"
}
]
}]
3.Here is my Entity 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;
}
}
If you want to delete a field you can put it a null and use @JsonInclude(Include.NON_NULL)
on your class as follow:
@Service
@Entity
@Table
@JsonInclude(Include.NON_NULL)
public class Family {
//class
}
If you can handle that you should substitute private List<Family> children = new ArrayList<>();
with private List<Family> children;
and initialize it when you are going to use it.
Just a question: Why are you using @Service on your entity class?