My rest controller returns the same object inifinite times for object USER. Other objects which do not require USER object works fine. What is wrong? If i remove some getters/setters it works ok, but i actually need everything here... Spring throws an exceptions - StackOverFlow exc and Java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
EDIT: I figured out that the problem IS "getRoles()" - unfortunately, it is very important for me and I need to include it in JSON... How to make it work?
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "first_name")
private String firstName;
private String surname;
private String email;
private String password;
@ManyToMany
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(
name = "role_id", referencedColumnName = "id"))
private List<Role> roles;
@JsonIgnore
@ManyToMany
@JoinTable(
name="users_documents",
joinColumns = @JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(
name="document_id", referencedColumnName = "id"))
private List<Document> usersDocuments;
@OneToMany(mappedBy="user")
private List<Document> sharedDocuments;
public User() {
}
public User(String firstName, String surname, String email, String password) {
this.firstName = firstName;
this.surname = surname;
this.email = email;
this.password = password;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setEmail(String email) {
this.email = email;
}
public void setPassword(String password) {
this.password = password;
}
public List<Role> getRoles() {
return roles;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return getId() == user.getId();
}
@Override
public int hashCode() {
return Objects.hash(getId());
}
@Override
public String toString() {
return "User{" +
"firstName='" + firstName + '\'' +
", surname='" + surname + '\'' +
", email='" + email + '\'' +
", roles=" + roles +
'}';
}
}