我使用Jackson JSON来序列化Game
对象,但它会引发无限循环异常,因为Position
类包含Island
类作为Game
类。
根据此article(第3部分),我尝试将注释@JsonManagedReference
添加到Game
的{{1}}字段和Island
到@JsonBackReference
' Position
字段。
序列化运行正常。但是,当我反序列化时抛出以下异常:
Island
如何正确序列化/反序列化Invalid type definition for type Position;: Can not bind back references as Creator parameters: type Position (reference 'defaultReference', parameter index #0)
对象?
这是我班级的片段。
游戏类
Game
玩家类
public class Game {
@JsonManagedReference
private Island island;
private Player player;
@JsonCreator
public Game(@JsonProperty("island")Island island, @JsonProperty("player")Player player) {
this.island = island;
this.player = player;
}
// other code ignored
}
职位等级
public class Player {
private Position position;
private String name;
@JsonCreator
public Player(@JsonProperty("position")Position position, @JsonProperty("name")String name) {
this.position = position;
this.name = name;
}
// other code ignored
}
Island class
public class Position {
private int row;
private int column;
@JsonBackReference
private Island island
@JsonCreator
public Position(@JsonProperty("island")Island island, @JsonProperty("row")int row, @JsonProperty("column")int column) {
this.island = island;
this.row = row;
this.column = column;
}
// other code ignored
}