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
类型具有由 Room
和 User
组成的嵌入式 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])"
}