java 无限递归,关于Java:如何解决无限递归(org.codehaus.jackson.map.JsonMappingException)...

当我从User调用findById方法时,会收到结果,但是当我尝试转换Web服务的返回值时,会抛出此异常:

org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.empsys.user.User["locations"]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location["contact"]->com.empsys.user.User["locations"]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location["contact"]->com.empsys.user.User["locations"]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location["contact"]->com.empsys.user.User["locations"]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location["contact"] ...

类关系为:

Contact类-创建该类以表示系统上的多种联系人。

@Entity

@Table(name ="CONTACT", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})

@Inheritance(strategy = InheritanceType.JOINED)

@DiscriminatorColumn(name ="CONTACT_TYPE")

public class Contact implements Serializable {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="contact")

@Column(nullable = true)

@JsonManagedReference

private List locations;

...

类User-创建该类是为了向用户表示特定信息

@Entity

@Table(name ="USER")

@DiscriminatorValue("USER")

public class User extends Contact {

...

类位置-创建该类以表示用户和系统其他联系人的地址

@Entity

@Table(name ="LOCATION", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})

public class Location implements Serializable {

@ManyToOne

@JoinColumn(name ="contact_id", nullable = false)

@JsonBackReference

private Contact contact;

...

使用的依赖项:

(com.sun.jersey)jersey-json:版本1.18

(com.fasterxml.jackson.datatype)jackson-datatype-hibernate4:版本:

2.4.0

有人可以帮我吗?

与Jackson JSON和Hibernate JPA问题进行的无限递归的可能重复项

我知道这是一篇很老的文章,但是对于那些将来要来这里的人来说,也许您应该看看这篇文章。

从Jackson 1.6开始,您可以使用@JsonManagedReference和@JsonBackReference。

我遇到了这样的问题。 在我的情况下,我在创建响应JSON时不希望导航的属性上使用@JsonIgnore。

我需要在此属性中导航,但是我已经尝试使用@JsonIgnore前进,但仍会发生异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值