I'm having a problem when i try to do a get request to my /users endpoint, instead of returning what i want to, returns this error below:
2018-07-07 17:00:06.636 ERROR 294108 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"])] with root cause
java.lang.StackOverflowError: null
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_171]
at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~
my endpoint method:
@Override
public ResponseEntity<List<M>> findAll() {
List<M> models = repository.findAll();
if(models == null || models.isEmpty()){
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(models);
}
User model:
@Entity
@Table(name = "UN_USERS")
public class User extends AbstractModel {
@Column(name = "USERNAME", unique = true)
private String username;
@Column(name = "UUID")
private String uuid;
@Column(name = "COINS")
private long coins;
@Column(name = "TOKENS")
private long tokens;
@ManyToOne
@JoinColumn(name = "GROUP_ID", nullable = true)
private Group group;
//getters & setters...
}
Group model:
@Entity
@Table(name="UN_GROUPS")
public class Group extends AbstractModel{
private String prefix;
@OneToMany(mappedBy = "group")
private Set<User> users;
@ManyToMany(mappedBy = "groups")
private Set<Permission> permissions;
}
What could possible give this error and how can i fix?
By analyzing the error message, this part specifically:
through reference chain: io.union.restapi.domain.Group["users"]->org.hibernate.collection.internal.PersistentSet[0]->io.union.restapi.domain.User["group"]->io.union.restapi.domain.Group["users"]
we got that the reference chain forms a cycle. You've run into the Jackson infinite recursion problem and there are several ways to solve it. You can find a good article that covers the ways to deal with it here