gpt4 book ai didi

spring-data-neo4j - Neo4j RelationshipEntities 和 Spring Data Rest 的循环 JSON 序列化问题

转载 作者:行者123 更新时间:2023-12-01 04:52:00 26 4
gpt4 key购买 nike

我在通过 Spring Data Rest 将 @RelationshipEntities 序列化为 JSON 时遇到问题。每当我创建@RelationshipEntity 时,我都会遇到将图形序列化为 JSON 的无限递归。

使用 JSOG 尝试呈现图形只会导致不同的、格式错误的 JSON 响应。

虽然我可以通过使用 @JsonManagedReference 来避免这个问题,但它并没有解决问题,因为我想公开两个节点的关系。

我创建了一个简单的应用程序来展示这个问题。可以在这里找到:https://github.com/cyclomaniac/neo4j-spring-data-rest-cyclic

它使用单个关系实体 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;

...

球员位置:
@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

我的假设是我选择了一种糟糕的方式来实现这种关系,尽管它感觉相当简单。我很感激有关如何从 Team 和 Player 节点正确公开这种关系的任何提示,最好是通过 Spring Data Rest。

最佳答案

尝试使用 @JsonIgnore 或配对进行注释:@JsonBackReference 和 @JsonManagedReference

关于spring-data-neo4j - Neo4j RelationshipEntities 和 Spring Data Rest 的循环 JSON 序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40430753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com