Neo4j RelationshipEntities and the cyclical JSON serialization issue with Spring Data Rest

I am having a problem with serializing @RelationshipEntities in JSON via Spring Data Rest. Whenever I create @RelationshipEntity, I run infinite recursion when serializing the graph in JSON.

Using JSOG to render the graph leads to a different, incorrect JSON response.

Although I can avoid the problem using @JsonManagedReference, it does not solve the problem, since I would like to expose the connection with both nodes.

I created a simple application that demonstrates the problem. It can be found here: https://github.com/cyclomaniac/neo4j-spring-data-rest-cyclic

It implements very simple Team and Player NodeEntities commands with one RelationshipEntity element, PlayerPosition.

Player:

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

    ...

Team:

@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, /teams 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, Team Player.

+4
1

@JsonIgnore : @JsonBackReference @JsonManagedReference

+1

Source: https://habr.com/ru/post/1659869/


All Articles