I have two SQL tables as follow:
users
id
username
password
...and more irrelevant stuff
activities
id
name
description
users
I created two entities classes with their Repository (with the anotation @RepositoryRestResource)
@Entity
public class Users {
@Id
private int id;
private String username;
private String password;
@OneToMany(mappedBy = "users")
private Set<Activities> activities;
public Set<Activities> getActivities() {
return activities;
}
//constructors && getters setters
and
@Entity
public class Activities {
@Id
private int id;
@ManyToOne
@JoinColumn(name = "users")
private Users users;
private String name;
private String description;
//constructors && get
I created an endpoint to test the workflow, by creating an activity. It's all hardcoded as follow
@RequestMapping("/calendar")
public Set<Activities> goToCalendar(Authentication authentication){
Users user = unbreakableService.isAuth(authentication);
Activities activities = new Activities(user,"karate", "entrenobueno");
activitiesRespository.save(activities);
return usersRepository.findByUsername(user.getUsername()).getActivities();
}
The code is saving the activity correctly, but, when it's supposed to return the list of activities, it returns an infinity recursive information, like:
[{"id":161,"users":{"id":121,"username":"Ojka","password":"$2a$10$Z/bj.5JvQmPquJ67fqF.leUyBkkXJ/2x3z/.bUon8YsIAdV8HMnoG","email":"ojka","pushup":99999,"pullup":null,"handstandhold":9999,"handstandpushup":null,"frontlever":null,"backlever":null,"activities":[{"id":161,"users":{"id":121,"username":"Ojka","password":"$2a$10$Z/bj.5JvQmPquJ67fqF.leUyBkkXJ/2x3z/.bUon8YsIAdV8HMnoG","email":"ojka","pushup":99999,"pullup":null,"handstandhold":9999,"handstandpushup":null,"frontlever":null,"backlever":null,"activities":[{"id":161,"users":{"id":121,"username":"Ojka","password":"$2a$10$Z/bj.5JvQmPquJ67fqF.leUyBkkXJ/2x3z/.bUon8YsIAdV8HMnoG","email":"ojka","pushup":99999,"pullup":null,"handstandhold":9999,"handstandpushup":null,"frontlever":null,"backlever":null,"activities":[{"id":161,"users":{"id":121,"username":"Ojka","password":"$2a$10$Z/bj.5JvQmPquJ67fqF.leUyBkkXJ/2x3z/.bUon8YsIAdV8HMnoG","email":"ojka","pushup":99999,"pullup":null,"handstandhold":9999,"handstandpushup":null,"frontl
I can't read the whole log since the console doesn't show that much information. This is the first error I can see
Failure while trying to resolve exception [org.springframework.http.converter.HttpMessageNotWritableException]
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed.
And this is the last line of the log
2020-01-25 20:48:54.795 ERROR 19924 --- [nio-8080-exec-2] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/api/calendar] and exception [Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.unbreakable.unbreakable.persistance.Users["activities"]->org.hibernate.collection.internal.PersistentSet[0]->com.unbreakable.unbreakable.persistance.Activities["users"]->com.unbreakable.unbreakable.persistance.Users["activities"]->org.hibernate.collection.internal.Persi....
If I check the table activities (SELECT * FROM ACTIVITIES) there's no infinity information. Just a few activities (the ones I created testing it).
I believe, it should return a Set of activities that the user has.
You can suppress the output in json in a couple of ways:
@JsonIgnore over the field with the relationship (activities or users).users with @JsonBackReference and activities with @JsonManagedReferenceIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With