2012-08-07 131 views
0

我目前正在研究一種使用泛型並且很複雜的模型。我明白類似的問題已經得到解答,但他們沒有一個明顯回答我的問題。使用Jackson對Java泛型進行序列化和反序列化:StackOverflowError

這裏是我的模型:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.WRAPPER_OBJECT, property = "type") 
@JsonSubTypes(
       { 
        @Type(value = Cls2.class, name = "Cls2") 
       }) 
abstract class Cls1<T> implements Serializable 
{ 
    private T myObj; 

    public T getMyObj() 
    { 
     return myObj; 
    } 

    public Cls1(T obj) 
    { 
     myObj = obj; 
    } 
    @JsonTypeName("Cls2") 
    public static class Cls2<E extends Int1> extends Cls1<E> implements Serializable 
    { 
     public Cls2() 
     { 
      super(null); 
     } 
    } 
} 

@JsonTypeName("ChildContainer") 
class ChildContainer extends ParentContainer<OtherBean> 
{ 

} 

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.WRAPPER_OBJECT, property = "type") 
@JsonSubTypes(
       { 
        @Type(value = ChildContainer.class, name = "ChildContainer") 
       }) 
class ParentContainer<T extends RootBean> implements Int1 
{ 

} 

@JsonTypeName("OtherBean") 
class OtherBean extends RootBean 
{ 

} 

@JsonTypeName("RootBean") 
class RootBean implements Int1 
{ 

} 

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.WRAPPER_OBJECT, property = "type") 
@JsonSubTypes(
       { 
        @Type(value = RootBean.class, name = "RootBean"), 
        @Type(value = OtherBean.class, name = "OtherBean") 
       }) 
interface Int1 extends Serializable 
{ 

} 

我的目標是序列化和使用傑克遜如下deserialze:

public static void main(String[] args) throws Exception 
    { 

     Cls2<ChildContainer> req = new Cls2<ChildContainer>(); 

     File file = new File("==some-file-path=="); 

     ObjectMapper mapper = new ObjectMapper(); 

     mapper.writeValue(file, req); 

     //read it back using mapper.readValue(file, clazz) --Not sure about this 
    } 

我序列化過程中得到以下java.lang.StackOverflowError的:

Exception in thread "main" java.lang.StackOverflowError 
    at java.lang.Class.getDeclaringClass(Native Method) 
    at org.codehaus.jackson.map.type.TypeBindings._resolveBindings(TypeBindings.java:290) 
    at org.codehaus.jackson.map.type.TypeBindings._resolve(TypeBindings.java:221) 
    at org.codehaus.jackson.map.type.TypeBindings.findType(TypeBindings.java:138) 
    at org.codehaus.jackson.map.type.TypeFactory._fromVariable(TypeFactory.java:951) 
    at org.codehaus.jackson.map.type.TypeFactory._constructType(TypeFactory.java:493) 
    at org.codehaus.jackson.map.type.TypeFactory.findTypeParameters(TypeFactory.java:423) 
    at org.codehaus.jackson.map.type.TypeFactory.findTypeParameters(TypeFactory.java:395) 
    at org.codehaus.jackson.map.type.TypeBindings._resolveBindings(TypeBindings.java:299) 
    at org.codehaus.jackson.map.type.TypeBindings._resolveBindings(TypeBindings.java:290) 
    at org.codehaus.jackson.map.type.TypeBindings._resolve(TypeBindings.java:221) 
    at org.codehaus.jackson.map.type.TypeBindings.findType(TypeBindings.java:138) 
    at org.codehaus.jackson.map.type.TypeFactory._fromVariable(TypeFactory.java:951) 
    at org.codehaus.jackson.map.type.TypeFactory._constructType(TypeFactory.java:493) 
    at org.codehaus.jackson.map.type.TypeFactory.findTypeParameters(TypeFactory.java:423) 
    at org.codehaus.jackson.map.type.TypeFactory.findTypeParameters(TypeFactory.java:395) 
    at org.codehaus.jackson.map.type.TypeBindings._resolveBindings(TypeBindings.java:299) 
    at org.codehaus.jackson.map.type.TypeBindings._resolveBindings(TypeBindings.java:290) 

任何幫助深表謝意。

+0

從錯誤消息或調試器中捕獲堆棧(重複)部分,肯定有助於確定發生圓形的位置。 – 2012-08-07 16:18:37

+0

@AndrzejDoyle我已經用stacktrace更新了帖子 – Chris 2012-08-07 16:21:31

+1

解析類型中有一個無限遞歸,這似乎是Jackson中的一個bug。嘗試將'Cls2'提取到獨立的類中。 – BalusC 2012-08-07 16:24:17

回答

1

stacktrace建議解決Cls2類型的無限遞歸,它擴展了它本身嵌套的類。這似乎是Jackson中的一個角落案例錯誤(report it!)。與此同時,將Cls2提取到獨立類中,而不是將它嵌套在其超類中,可以解決此問題。

相關問題