java - Jackson - 循环依赖导致反序列化失败

标签 java json serialization jackson

好的,所以我正在尝试使用 jackson json 转换器测试一些东西。 我正在尝试模拟图形行为,所以这些是我的 POJO 实体

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ParentEntity implements java.io.Serializable
{   
    private String id;
    private String description;
    private ParentEntity parentEntity;
    private List<ParentEntity> parentEntities = new ArrayList<ParentEntity>(0);
    private List<ChildEntity> children = new ArrayList<ChildEntity>(0);
    // ... getters and setters
}

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ChildEntity implements java.io.Serializable
{
    private String id;
    private String description;
    private ParentEntity parent;
    // ... getters and setters
}

需要这些标签以避免序列化异常。 当我尝试序列化一个对象(无论是在文件上还是在简单的字符串上)时,一切正常。但是,当我尝试反序列化该对象时,它会引发异常。这是简单的测试方法(为简单起见省略了try/catch)

{
    // create some entities, assigning them some values
    ParentEntity pe = new ParentEntity();
    pe.setId("1");
    pe.setDescription("first parent");

    ChildEntity ce1 = new ChildEntity();
    ce1.setId("1");
    ce1.setDescription("first child");
    ce1.setParent(pe);

    ChildEntity ce2 = new ChildEntity();
    ce2.setId("2");
    ce2.setDescription("second child");
    ce2.setParent(pe);

    pe.getChildren().add(ce1);
    pe.getChildren().add(ce2);

    ParentEntity pe2 = new ParentEntity();
    pe2.setId("2");
    pe2.setDescription("second parent");
    pe2.setParentEntity(pe);
    pe.getParentEntities().add(pe2);

    // serialization
    ObjectMapper mapper = new ObjectMapper();
    File f = new File("parent_entity.json");
    // write to file
        mapper.writeValue(f, pe);
    // write to string
    String s = mapper.writeValueAsString(pe);
    // deserialization
    // read from file
    ParentEntity pe3 = mapper.readValue(f,ParentEntity.class);
    // read from string
    ParentEntity pe4 = mapper.readValue(s, ParentEntity.class);         
}

这是抛出的异常(当然,重复两次)

com.fasterxml.jackson.databind.JsonMappingException: Already had POJO for id (java.lang.String) [com.fasterxml.jackson.annotation.ObjectIdGenerator$IdKey@3372bb3f] (through reference chain: ParentEntity["children"]->java.util.ArrayList[0]->ChildEntity["id"])
...stacktrace...
Caused by: java.lang.IllegalStateException: Already had POJO for id (java.lang.String) [com.fasterxml.jackson.annotation.ObjectIdGenerator$IdKey@3372bb3f]
...stacktrace...

那么,问题的原因是什么?我该如何修复它?我还需要其他注释吗?

最佳答案

实际上,问题似乎出在“id”属性上。由于两个不同实体的属性名称相同,因此在反序列化期间会出现一些问题。不知道这是否有意义,但我解决了将 ParentEntity 的 JsonIdentityInfo 标记更改为

的问题
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = ParentEntity.class))

当然,我还用 scope=ChildEntity.class 更改了 ChildEntity 的范围 按照建议here

顺便说一句,我愿意接受新的答案和建议。

关于java - Jackson - 循环依赖导致反序列化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26698434/

相关文章:

java - 从源代码构建 Android - 文件未找到错误

javascript - 使用 Knockout JS 将多个不同的 JSON 源映射到一个 View 模型

java - 使用java从json中分割时间值

java - 从字节数组反序列化后 Xml 文档签名无效

没有列名的javascript序列化

java - 对象从 java 客户端到 WCF Web 服务,反序列化时出错

java - GridBag布局和绘图

java - 隔离 Maven 依赖项

java - 使用准备好的语句时出错

php - HTML通过JSON输出,通过ajax激活