我有像这样的双向关系......
Person.java
public class Person{
@JsonIgnore
@OneToMany(targetEntity=PersonOrganization.class, cascade=CascadeType.ALL,
fetch=FetchType.EAGER, mappedBy="person")
private Set<PeopleOrg> organization;
.....
}
Run Code Online (Sandbox Code Playgroud)
PersonOrganization.java
public class PersonOrganization{
@JsonIgnore
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PERSONID", nullable=false)
private Person person;
}
Run Code Online (Sandbox Code Playgroud)
即使使用@JsonIgnore注释,我在尝试检索Person记录时也会收到无限递归错误.我在1.6版本中尝试过新的注释.@JsonBackReference和@JsonManagedReference.即使这样,我也会得到无限的递归..
随着@JsonBackReference("person-organization")上Person和@JsonManagedReference("person-organization")上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]...
Run Code Online (Sandbox Code Playgroud)
即使我交换了注释,我仍然会遇到此异常..如果映射或我使用JSON注释的方式有问题,请告诉我.谢谢
dwi*_*wi2 40
我以前碰到过这个.但是在将@JsonIgnore从私有字段移动到该字段的getter之后,无限递归消失了.所以我的猜测是,@ JsonIgnore可能无法在私人领域工作.但是,javadoc或Jackson Java JSON处理器的教程没有提到这一点,所以我不能100%肯定.仅供参考.
小智 10
以下链接说明您应该注释JSON工具用于遍历对象图的方法,以指示它忽略遍历.
http://jackson.codehaus.org/1.0.1/javadoc/org/codehaus/jackson/annotate/JsonIgnore.html
在我的情况下,我有两个相关的对象,如Product < - > ProductImage.所以JSON解析器进入一个无限循环,在下面输出@JsonIgnore注释来获取方法
@JsonIgnore
public Product getImageOfProduct() {
return imageOfProduct;
}
Run Code Online (Sandbox Code Playgroud)
在ProductImage和
@JsonIgnore
public Set<ProductImage> getProductImages() {
return productImages;
}
Run Code Online (Sandbox Code Playgroud)
在产品中.
有了注释,事情就好了.
我知道这个问题并不是专门针对Spring Data REST的,但我在Spring Data REST的上下文中遇到了这个异常,并希望分享问题所在.我有一个涉及没有存储库的实体的双向关系.创建存储库使循环消失.