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

  • Post author:
  • Post category:Bugs
  • Post comments:0 Comments
  • Post last modified:November 7, 2021
  • Reading time:2 mins read

Its a common problem when working one to many, many to many jpa relation. 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.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<Authority> authorities;
  
 

Authority.class


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

Hope it helps

Leave a Reply