Spring Data Rest歧义关联异常

9
新增的LinkCollectingAssociationHandler由于在我的领域类中存在一个模糊的关联,导致它抛出了一个MappingException异常。
链接数组如下: [<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"] 在尝试添加另一个“room”关系时,它会抛出异常。
问题是似乎它正在添加链接到我明确标记为@RestResource(exported = false)的关系中。
以下是我认为引起此问题的一个关系示例:
@JsonIgnore
@RestResource(exported = false)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.room", cascade = {CascadeType.REMOVE})
private Set<RoomsByUserAccessView> usersThatCanDisplay = new HashSet<>(); 

RoomsByUserAccessView 类型具有由 RoomUser 组成的嵌入式 id。

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

@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时正确忽略这些关联?

在 DATAREST-262 (https://github.com/spring-projects/spring-data-rest/commit/1d53e84cae3d09b09c4b5a9a4caf438701527550) 之前,我的代码是可以正常工作的。

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

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


1
似乎是一个重复的问题,请查看这个链接 https://dev59.com/RWAf5IYBdhLWcg3wo0Bn - Prog Mania
嘿,你解决了这个问题吗?我也试图在JSON中不显示某些值,但是仍然抛出异常 : / - hi_my_name_is
我真的记不清了... 我相信这个问题在更近版本的Spring Data Rest中已经被修复了。至少我现在似乎没有这个问题了,而且@RestResource(exported = false)似乎是不必要的,因为现在@JsonIgnore已经被尊重了。 - Ethan Anderson
问题出在@RestResource(exported = false)上。它会导致相关的链接和正文被嵌入到父资源中,这可能会导致rel冲突。我仍然不清楚这是否是预期的SDR行为,但事实就是如此。 - user41871
1
我为此创建了一个 JIRA 问题:https://jira.spring.io/browse/DATAREST-520 - user41871
2个回答

5
我遇到了非常类似的问题。在两个实体之间添加双向关系时,我得到了一个异常“无法编写JSON:检测到具有相同关系类型的多个关联链接!”,尝试了一些在这里找到的解决方案(包括@JsonIgnore、@JsonIdentity、@RestResource),我还尝试了Oliver提供的方法。关系已经正确地定义为@JsonManagedReference和@JsonBackReference。但是没有什么帮助。
最后,我设法理解Spring Data Rest正在尝试确定相关实体是否可链接(在构建所请求实体的JSON时),为此它正在寻找处理相关实体的存储库。在为相关实体添加存储库后,它就像魔术一样运行了。
(我认为在添加存储库后,映射RepositoryAwareResourceInformation正在进行)。

为该实体创建(这是我在调试中看到的)。


被同样的情况绊倒了! - Tahir Akhtar

0

我遇到了这个问题,像Ron建议的那样解决了它,但我想多谈一下。我在读Ron的答案前几次没有完全理解...

@NodeEntity
public class Player extends Entity

@NodeEntity
public class PlayerTrade extends Entity

@NodeEntity
public class Trade extends Entity

我有玩家(Player)和交易(Trade)的存储库,但没有玩家交易(PlayerTrade)的存储库:

@RepositoryRestResource
public interface PlayerRepository  extends GraphRepository<Player> {

@RepositoryRestResource
public interface TradeRepository extends GraphRepository<Trade> {

我添加最后一个仓库后它就能工作了。

@RepositoryRestResource
public interface PlayerTradeRepository extends GraphRepository<PlayerTrade> 

我尝试使用@RestResource与rel或excluded,但无法调整它。这是否意味着我们JSON图中的每个实体都必须有一个存储库?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接