Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - JSON infinite recursion

Tags:

I have bi-directional relationship like this...

Person.java

 public class Person{      @JsonIgnore     @OneToMany(targetEntity=PersonOrganization.class, cascade=CascadeType.ALL,         fetch=FetchType.EAGER, mappedBy="person")     private Set<PeopleOrg> organization;     .....  } 

PersonOrganization.java

  public class PersonOrganization{      @JsonIgnore @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="PERSONID", nullable=false) private Person person;   } 

Even with @JsonIgnore annotation I am getting infinite recursion error when trying to retrieve Person records. I have tried new annotations in 1.6 version. @JsonBackReference and @JsonManagedReference. Even then I am getting infinite recursion..

With @JsonBackReference("person-organization") on Person and @JsonManagedReference("person-organization") on PersonOrganization

org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.entity.Person["organization"]->org.hibernate.collection.PersistentSet[0]->com.entity.PersonOrganization["person"]->com.entity.Person["organization"]->org.hibernate.collection.PersistentSet[0]... 

Even If I interchange the annotations, I am still getting this exception.. Please let me know if there is something wrong with the mappings or the way I am using JSON annotations. Thanks

like image 915
RKodakandla Avatar asked Jan 27 '12 18:01

RKodakandla


Video Answer


1 Answers

I've run into this before. But after moving @JsonIgnore from private field to getter of the field, infinite recursion is gone. So my wild guess is that @JsonIgnore might no work on private field. However, javadoc or tutorial of Jackson Java JSON-processor do not mention about this, so I cannot be 100% sure. Just for your information.

like image 97
dwi2 Avatar answered Sep 23 '22 19:09

dwi2