4

Spring Data Restを経由して@RelationshipEntitiesをJSONにシリアル化する際に問題が発生しました。 @RelationshipEntityを作成するたびに、グラフをJSONにシリアル化することで無限の再帰を実行します。Neo4j RelationshipEntitiesとSpringデータレストのサイクリックJSONシリアライゼーションの問題

JSOGを使用してグラフをレンダリングしようとすると、異なる形式のJSON応答が返されます。

@JsonManagedReferenceを使用して問題を回避することはできますが、両方のノードからリレーションシップを公開したいので問題は解決しません。

問題を示す簡単なアプリケーションを作成しました。 https://github.com/cyclomaniac/neo4j-spring-data-rest-cyclic

非常に基本的なチームとプレーヤーのNodeEntitiesを1つのRelationshipEntity、PlayerPositionで実装しています。

プレーヤー:

@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; 

    ... 

/チームはJSOGと次の出力で結果をエンドポイント打つ、GraphRepositoryまで有線場所:

{ 
    "_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を介して、この関係を適切に公開する方法についてのヒントをお待ちしております。

答えて

1

@JsonIgnoreまたは@JsonBackReferenceと@JsonManagedReferenceで注釈を付けるようにしてください。

関連する問題