我遇到了通过Spring Data Rest将@RelationshipEntities序列化为JSON的问题。每当我创建@RelationshipEntity时,我都会在将图形序列化为JSON时遇到无限递归。
使用JSOG尝试渲染图形只会导致不同的,错误的JSON响应。
虽然我可以通过使用@JsonManagedReference来避免这个问题,但它并没有解决问题,因为我想从两个节点公开关系。
我创建了一个展示问题的简单应用程序。可在此处找到:https://github.com/cyclomaniac/neo4j-spring-data-rest-cyclic
它使用单个RelationshipEntity,PlayerPosition实现非常基本的Team和Player 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时,点击/ 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节点正确展示这种关系的任何提示。
答案 0 :(得分:1)
尝试使用@JsonIgnore注释或配对:@JsonBackReference和@JsonManagedReference