【发布时间】:2018-06-03 18:51:40
【问题描述】:
如何解决JSON的循环依赖:
类:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uniqueID")
final public class TestNode {
private final String uniqueID;
private final TestNode testNode; //Circular dependency
}
主要:
private final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(JsonOutputFile, TestNode.class); // Write object to file
*如果我使用 JsonIdentityInfo 那么在将对象写入文件时我会得到结果
{ "uniqueID": "1", "testNode": "1"}
*如果我不使用JsonIdentityInfo,那么我会得到正确的 JSON,即 testNode 是正确的,但我会收到以下错误:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: ["testNode"] ...
PS:我使用的是以下版本: Jackson:3-SNAPSHOT,Java:8,Spring:4.3
【问题讨论】:
-
你检查异常是什么意思了吗?