我遇到了通过Spring Data Rest将@RelationshipEntities序列化为JSON的问题。无论何时创建@RelationshipEntity,我都会在将图形序列化为JSON时遇到无限递归。Neo4j RelationshipEntities和Spring Data Rest的循环JSON序列化问题
使用JSOG试图渲染图形只会产生不同的,格式不正确的JSON响应。
虽然我可以通过使用@JsonManagedReference避免这个问题,但它不能解决问题,因为我想从两个节点公开关系。
我创建了一个展示该问题的简单应用程序。它可以在这里找到:https://github.com/cyclomaniac/neo4j-spring-data-rest-cyclic
它实现了与一个RelationshipEntity,PlayerPosition非常基本的团队和播放器NodeEntities。
球员:
@NodeEntity
@JsonIdentityInfo(generator= JSOGGenerator.class)
public class Player {
@GraphId
@JsonProperty("id")
private Long id;
private String name;
private String number;
@Relationship(type = "PLAYS_ON")
private PlayerPosition position;
...
团队:
@NodeEntity
@JsonIdentityInfo(generator= JSOGGenerator.class)
public class Team {
@GraphId
@JsonProperty("id")
private Long id;
private String name;
@Relationship(type = "PLAYS_ON", direction = Relationship.INCOMING)
Set<PlayerPosition> teamPlayers;
...
PlayerPosition:
@RelationshipEntity(type="PLAYS_ON")
@JsonIdentityInfo(generator= JSOGGenerator.class)
public class PlayerPosition {
@GraphId
@JsonProperty("id")
private Long id;
private String position;
@StartNode
private Player player;
@EndNode
private Team team;
...
当有线高达GraphRepository,击中/支终点结果与JSOG以下输出到位:
{
"_embedded" : {
"teams" : [ {
"@id" : "1",
"name" : "Cubs",
"teamPlayers" : [ {
"@id" : "2",
"position" : "Catcher",
"player" : {
"@id" : "3"
请注意,JSON过早结束。服务器抛出一个异常:
2016-11-04 15:48:03.495 WARN 12287 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message:
org.springframework.http.converter.HttpMessageNotWritableException:
Could not write content: Can not start an object,
expecting field name; nested exception is
com.fasterxml.jackson.core.JsonGenerationException:
Can not start an object, expecting field name
我的假设是,我已经选择了实现关系的好办法,虽然它让人感觉相当简单。我很感激任何关于如何正确地通过Spring Data Rest从团队和播放器节点中公开此关系的提示。