背景:
使用spring-data-neo4j 进行crud 数据。在进行大批数据入库时,数据入库到一半,突然提示
Neo.TransientError.Network.CommunicationError"; Code: Neo.TransientError.Network.CommunicationError; Description: Can not start an object, expecting field name
本次实体是(已经真实信息进行处理):
@Getter
@Setter
@ToString
@NodeEntity(label = "person")
public class Person implements Comparable<Person> {
@GraphId
Long id;
@Property(name = "name")
private String name;
@Property(name = "code")
private String code;
@DateLong
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date create;
@Relationship(type = "standard")
private Set<Standard> standardList = new HashSet<>();
@Relationship(type = "line")
private Set<LineInfo> lineInfo = new HashSet<>();
.....
}
@Getter
@Setter
@RelationshipEntity(type = "Standard")
public class Standard {
@GraphId
private Long id;
@StartNode
@JsonBackReference
private Site startSite;
@EndNode
@JsonBackReference
private Site endSite;
private String standCode;
}
开始还以为是有脏数据,比如有空的,非法的数据,先对数据进行排查过滤处理,保证入库的数据关键字段都是合法的。重试。发现问题依旧存在。
经过排查发现。每当我创建@RelationshipEntity时,在dubug 查看数据时可以会在将图形序列化为JSON时遇到无限递归。
虽然通过 @JsonBackReference 避免了序列化,但是貌似还是不生效。当少量的数据时,序列化正常。当数据量大时,跑到一半就报Can not start an object, expecting field name。
通过查阅相关的资料,发现使用JSOG尝试呈现图形只会导致不同的,格式错误的JSON响应。
最后看到github 的小demo (https://github.com/cyclomaniac/neo4j-spring-data-rest-cyclic
这个demo 演示使用小测试用例呈现循环数据的问题。
最终是通过 使用@JsonIgnore注释或配对:@JsonBackReference和@JsonManagedReference
解决循环数据的问题。
最后实体是:
@Getter
@Setter
@RelationshipEntity(type = "Standard")
public class Standard {
@GraphId
private Long id;
@StartNode
@JsonIgnore
@JsonBackReference
private Site startSite;
@EndNode
@JsonIgnore
@JsonBackReference
private Site endSite;
private String standCode;
}
补充知识:
@JsonManagedReference和@JsonBackReference 序列化时,
@JsonBackReference标注的属性在会被忽略。
@JsonManagedReference标注的属性则会被序列化。
在序列化时,@JsonBackReference的作用相当于@JsonIgnore,此时可以没有@JsonManagedReference。
反序列化时,如果没有@JsonManagedReference,则不会自动注入@JsonBackReference标注的属性(被忽略的父或子);如果有@JsonManagedReference,则会自动注入自动注入@JsonBackReference标注的属性。