Spring Data Rest Ambiguous Association Exception

Eth*_*son 9 spring json spring-mvc spring-data-rest spring-hateoas

新添加的LinkCollectingAssociationHandlerMappingException由于我的域类中的模糊关联而引发的.

links数组如下所示:

[<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"]

它正在尝试在抛出异常时添加另一个"房间"关系.

问题是它似乎在添加我已明确标记的关系链接 @RestResource(exported = false)

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

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

该类型RoomsByUserAccessView具有由a Room和a 组成的嵌入式id User.

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

@JsonIgnore
@RestResource(exported = false)
private RoomsByUserAccessViewId pk = new RoomsByUserAccessViewId();
Run Code Online (Sandbox Code Playgroud)

和它的属性如下:

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

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

public RoomsByUserAccessViewId() {
    //
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

{ 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])" }

Hez*_*ger 5

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

我得到一个异常""无法写入JSON:检测到多个关联链接

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

(@JsonIgnore, @JsonIdentity, @RestResource我也试着做奥利弗提供的 )

(这个关系用@JsonManagedReference和正确定义@JsonBackReference)

什么都没有帮助.

最后,我设法理解Spring数据正在尝试

弄清楚相关实体是否可链接(同时尝试构建相关实体的json)

请求的实体)

(LinkCollectingAssociationHandler : doWithAssociation -> isLinkableAssociation)

为此,他正在寻找一个处理该问题的存储库

相关实体.为相关实体添加存储库后,就像魅力一样......

(我想在添加一个repo之后会出现一个映射RepositoryAwareResourceInformation

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