I was getting the error "Could not write JSON: Infinite recursion" when trying to access the endpoint http://localhost:8080/categoryOfPermissions. I've researched and found various solutions here (@JsonManagedReference / @JsonBackReference
, @JsonIgnore
, @JsonIdentityInfo
), but none of them seemed to work. Finally, I found an answer stating that it was necessary to change from Set to List, in order for the @JsonIdentityInfo
solution to work. I tested it an it really starts to work after changing from Set to List.
I thought that it was strange, but I found out something even stranger: after changing from Set to List, I removed the @JsonIdentityInfo
annotations and everything continued to work. In other words, all that I really needed to do was changing from Set to List to get rid of the exception. Nothing else. No need of any of the solutions :@JsonManagedReference / @JsonBackReference
, @JsonIgnore
, @JsonIdentityInfo
.
Below is the code that was producing the exception. All I had to do was changing private Set<Permission> permission
to private List<Permission> permission
.
I would like to know why, especially because I would prefer to use Set, in order to avoid Hibernate to use the "Bags" paradigm (which may cause some undesirable behaviors).
Permission.java :
@Entity
@Data
public class Permission{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotBlank
private String name;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_of_permission_id")
private CategoryOfPermission categoryOfPermission;
}
CategoryOfPermission.java :
@Entity
@Data
public class CategoryOfPermission{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotBlank
private String name;
@NotNull
@OneToMany(mappedBy = "categoryOfPermission", fetch=FetchType.LAZY)
private Set<Permission> permission;
}
CategoryOfPermissionRepo.java :
public interface CategoryOfPermissionRepo extends CrudRepository<CategoryOfPermission, Integer>{
}