[solved] at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize

It’s a common problem when working one to many, many to many JPA relations. Sometimes providing the bad configuration occurs infinite loop, as a result, the problem looks like

at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.10.jar:2.8.10]     
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.10.jar:2.8.10]     
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689) ~[jackson-databind-2.8.10.jar:2.8.10]     
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.10.jar:2.8.10]     
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.8.10.jar:2.8.10]     
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112) ~[jackson-databind-2.8.10.jar:2.8.10]     
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.8.10.jar:2.8.10]     
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.10.jar:2.8.10]

Solution:

So, it is basically JSON Serialisation, NOT THE JPA API. @JsonIgnore is the possible solution to get rid of this problem.

so Take the given working solution between User and Authority entity.

User.java class

 
 

 @ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
 @JoinTabl
         name = "users_authorities",
         joinColumns = {@JoinColumn(name = "user_id")},
         inverseJoinColumns = {@JoinColumn(name = "authority_id")}
         )
 
 @JsonIgnore
 private Collection authorities;
  
 

Authority.java


 @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy = "authorities")
 @JsonIgnore
 private Collection users; 

Hope it helps

Leave a Reply