【问题标题】:Spring Rest, JsonManagedReferences and cyclic referencesSpring Rest、JsonManagedReferences 和循环引用
【发布时间】: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[&quot;_embedded&quot;]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion

我会很高兴任何指针。

【问题讨论】:

    标签: spring rest spring-boot spring-hateoas


    【解决方案1】:

    您使用的是哪个 Jackson 版本? Jackson 2.2.3 没有这个问题。它会自动处理

    【讨论】:

    • 嗨,谢谢你还在看这个;)已经有一段时间了,我不再从事这个项目了,我不记得了。不过感谢您的关注!
    猜你喜欢
    • 2021-11-03
    • 2012-07-06
    • 2018-03-22
    • 2015-06-19
    • 1970-01-01
    • 2015-05-18
    • 2012-09-02
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多