【发布时间】:2015-07-23 10:58:14
【问题描述】:
我面临一个问题,即在将对象编组到 Json 时,使用 @JsonManagedReferences 和 @JsonBackReference 不会破坏无限递归循环。
我知道我可以尝试避免双向关系,但这不会很好。另一个不理想的解决方案是删除@RestResource(exported=false) 注释并点击仅提供一次的链接。
一个例子是:
@JsonManagedReference
@ManyToOne(cascade=CascadeType.ALL)
@RestResource(exported=false)
@JoinColumn(name="organisation_id")
private Organisation organisation;
与它在另一个类中的对应物:
@JsonBackReference
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JoinColumn(name ="organisation_id")
@RestResource(exported=false)
@Fetch(value = FetchMode.SUBSELECT)
private Set<OrganisationUnit> organisationUnits;
Bot 类有一个 @RepositoryRestResource,其中没有什么特别之处。
@RepositoryRestResource
public interface OrganisationRepository extends JpaRepository<Organisation, Long> {
public final static String ID_QUERY = "SELECT u FROM Organisation u where u.id=:objectId";
@Query(ID_QUERY)
public Organisation findObjectById(@Param("objectId") Long objectId);}
我知道 Jackson 2 应该能够处理各种情况,但在这种情况下它并不能解决问题。这是我不知道的已知行为吗? 请让我知道任何明显的缺陷,因为我对 JPA、Hibernate 或 Spring 的使用经验并不丰富。
提供的错误信息是:
There was an unexpected error (type=Internal Server Error, status=500). Could not write content: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.hateoas.PagedResources["_embedded"]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion
我会很高兴任何指针。
【问题讨论】:
标签: spring rest spring-boot spring-hateoas