2014-11-02 26 views
1

好的,所以我試圖用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 
} 

的標籤要求,以避免在序列化異常。 當我嘗試序列化一個對象(包括一個文件或一個簡單的字符串),一切正常。但是,當我嘗試反序列化對象時,它會引發異常。這是簡單的測試方法(試行爲了簡化省略/捕獲)

{ 
    // 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) [[email protected]f] (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) [[email protected]f] 
...stacktrace... 

那麼,什麼是該問題的原因?我該如何解決它?我是否需要其他註釋?

+0

我不是爲什麼,但如果你改變ce1和ce2的id,將不會拋出異常 – esprittn 2014-11-02 13:19:18

+0

是的,我會在幾分鐘後發佈一些消息 – tigerjack89 2014-11-02 14:10:18

回答

2

其實,它似乎是問題與「id」屬性。由於兩個不同實體的財產名稱相同,所以在反序列化過程中存在一些問題。不知道這是否有道理可言,但我解決了改變ParentEntity的JsonIdentityInfo標籤的問題

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

當然,我也改變ChildEntity的範圍與scope=ChildEntity.class 的建議here

我很樂意接受新的答案和建議。

+0

謝謝你的信息。你可以添加這行 mapper.configure(SerializationFeature.INDENT_OUTPUT,true);配置對象映射器的漂亮打印 – esprittn 2014-11-02 16:29:43

+1

除了上面的內容外,我還在fasterxml站點 上發現了其他附加功能 - 允許「空」POJO的序列化(沒有要序列化的屬性)(沒有此設置,在這些設置中拋出異常例) \t'mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);' -to寫java.util.Date,日曆作爲數(時間戳): \t \t'mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);' – tigerjack89 2014-11-02 16:35:56