Spring Data Rest歧义关联异常 [英] Spring Data Rest Ambiguous Association Exception

查看:119
本文介绍了Spring Data Rest歧义关联异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于域类中的关联不明确,新添加的LinkCollectingAssociationHandler抛出了MappingException.

The newly added LinkCollectingAssociationHandler is throwing a MappingException due to an ambiguous association in my domain class.

links数组看起来像这样:

The links array looks like this:

[<http://localhost:8080/rooms/2/roomGroups>;rel="roomGroups", <http://localhost:8080/rooms/2/userGroup>;rel="userGroup", <http://localhost:8080/rooms/2/room>;rel="room", <http://localhost:8080/rooms/2/originatingConferences>;rel="originatingConferences", <http://localhost:8080/rooms/2/user>;rel="user"]

当它引发异常时,它正在尝试添加另一个房间"关系.

And it is trying to add another 'room' relation when it throws the exception.

问题在于,它似乎正在向我用@RestResource(exported = false)

The issue is that it seems to be adding links to relations which I have explicitly marked with @RestResource(exported = false)

以下是我认为导致此问题的一种关系示例:

Here is an example of a relation which I believe is causing this issue:

@JsonIgnore
@RestResource(exported = false)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.room", cascade = {CascadeType.REMOVE})
private Set<RoomsByUserAccessView> usersThatCanDisplay = new HashSet<>(); 

类型RoomsByUserAccessView具有由RoomUser组成的嵌入式ID.

The type RoomsByUserAccessView has an embedded id made up of a Room and a User.

我还用以下注释了嵌入式id属性:

I have also annotated the embedded id property with:

@JsonIgnore
@RestResource(exported = false)
private RoomsByUserAccessViewId pk = new RoomsByUserAccessViewId();

及其属性如下:

@JsonIgnore
@RestResource(exported = false)
private Room room;

@JsonIgnore
@RestResource(exported = false)
private User userWithAccess;

public RoomsByUserAccessViewId() {
    //
}

在序列化为JSON时如何正确忽略这些关系?

How can I get it to ignore these relations properly when serializing to JSON?

我的代码在DATAREST-262( https://github.com/spring -projects/spring-data-rest/commit/1d53e84cae3d09b09c4b5a9a4caf438701527550 )

My code was working prior to DATAREST-262 (https://github.com/spring-projects/spring-data-rest/commit/1d53e84cae3d09b09c4b5a9a4caf438701527550)

当我尝试访问房间/端点时返回的完整错误消息如下:

The full error message returned when I try to visit the rooms/ endpoint is as follows:

{ timestamp: "2014-03-17T13:38:05.481-0500" error: "Internal Server Error" status: 500 exception: "org.springframework.http.converter.HttpMessageNotWritableException" message: "Could not write JSON: Detected multiple association links with same relation type! Disambiguate association @com.fasterxml.jackson.annotation.JsonIgnore(value=true) @javax.persistence.ManyToOne(fetch=EAGER, cascade=[], optional=true, targetEntity=void) @org.springframework.data.rest.core.annotation.RestResource(description=@org.springframework.data.rest.core.annotation.Description(value=), path=, exported=false, rel=) private com.renovo.schedulerapi.domain.Room com.renovo.schedulerapi.domain.RoomsByUserAccessViewId.room using @RestResource! (through reference chain: org.springframework.hateoas.PagedResources["content"]->java.util.UnmodifiableCollection[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Detected multiple association links with same relation type! Disambiguate association @com.fasterxml.jackson.annotation.JsonIgnore(value=true) @javax.persistence.ManyToOne(fetch=EAGER, cascade=[], optional=true, targetEntity=void) @org.springframework.data.rest.core.annotation.RestResource(description=@org.springframework.data.rest.core.annotation.Description(value=), path=, exported=false, rel=) private com.renovo.schedulerapi.domain.Room com.renovo.schedulerapi.domain.RoomsByUserAccessViewId.room using @RestResource! (through reference chain: org.springframework.hateoas.PagedResources["content"]->java.util.UnmodifiableCollection[0])" }

{ timestamp: "2014-03-17T13:38:05.481-0500" error: "Internal Server Error" status: 500 exception: "org.springframework.http.converter.HttpMessageNotWritableException" message: "Could not write JSON: Detected multiple association links with same relation type! Disambiguate association @com.fasterxml.jackson.annotation.JsonIgnore(value=true) @javax.persistence.ManyToOne(fetch=EAGER, cascade=[], optional=true, targetEntity=void) @org.springframework.data.rest.core.annotation.RestResource(description=@org.springframework.data.rest.core.annotation.Description(value=), path=, exported=false, rel=) private com.renovo.schedulerapi.domain.Room com.renovo.schedulerapi.domain.RoomsByUserAccessViewId.room using @RestResource! (through reference chain: org.springframework.hateoas.PagedResources["content"]->java.util.UnmodifiableCollection[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Detected multiple association links with same relation type! Disambiguate association @com.fasterxml.jackson.annotation.JsonIgnore(value=true) @javax.persistence.ManyToOne(fetch=EAGER, cascade=[], optional=true, targetEntity=void) @org.springframework.data.rest.core.annotation.RestResource(description=@org.springframework.data.rest.core.annotation.Description(value=), path=, exported=false, rel=) private com.renovo.schedulerapi.domain.Room com.renovo.schedulerapi.domain.RoomsByUserAccessViewId.room using @RestResource! (through reference chain: org.springframework.hateoas.PagedResources["content"]->java.util.UnmodifiableCollection[0])" }

推荐答案

我有一个非常相似的问题.在两个实体之间添加双向关系时

I had a very similar problem . When adding a bidirectional relationship between two entities

我遇到一个异常"无法编写JSON:检测到多个具有相同关联的关联链接

I got an exception ""Could not write JSON: Detected multiple association links with same

关系类型!",尝试一些我在这里找到的解决方案

relation type!" , While trying some solutions that i found here

(@JsonIgnore, @JsonIdentity, @RestResource,我还尝试做(该关系已使用@JsonManagedReference@JsonBackReference正确定义)

(The relation was properly defined with @JsonManagedReference and @JsonBackReference)

没有帮助.

最后,我设法了解到spring数据余量正在尝试

At the end i managed to understand that spring data rest is trying to

弄清楚相关实体是否可链接(在尝试构建

figure out if the related entity is linkable ( while trying to build the json of the

请求的实体

(LinkCollectingAssociationHandler : doWithAssociation -> isLinkableAssociation)

,为此,他正在寻找可处理

, For doing that he is looking for a repository that deals with the

相关实体.在为相关实体添加存储库之后..就像一个超级按钮..

related entity. After adding a repository for the related entity.. works like a charm..

(我想在添加一个仓库之后,正在映射一个RepositoryAwareResourceInformation

(I suppose that after adding a repo a mapping RepositoryAwareResourceInformation is being

为此实体创建的(这是我在调试时看到的).

created for this entity (that is what I saw at debug).

这篇关于Spring Data Rest歧义关联异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆