You are facing this issue because the Statemaster model contains the object of Districtmaster model, which itself contains the object of Statemaster model. This causes an infinite json recursion.
You can solve this issue by 3 methods.
1 – Create a DTO and include only the fields that you want to display in the response.
2 – You can use the @JsonManagedReference
and @JsonBackReference
annotations.
E.g. Add the @JsonManagedReference
annotation to the Statemaster model.
@JsonManagedReference
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="statemaster")
public Set<Districtmaster> getDistrictmasters() {
return this.districtmasters;
}
Add the @JsonBackReference
annotation to the Districtmaster model.
@JsonBackReference
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="district_of_state")
public Statemaster getStatemaster() {
return this.statemaster;
}
3 – You can use the @JsonIgnore
annotation on the getter or setter method.
@JsonIgnore
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="statemaster")
public Set<Districtmaster> getDistrictmasters() {
return this.districtmasters;
}
However, this approach will omit the set of Districtmaster from the response.