0

我目前正在研究一个使用泛型并且有点复杂的模型。我知道已经回答了类似的问题,但没有一个明确回答我的问题。

这是我的模型:

@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
{

}

我的目标是使用杰克逊进行序列化和反序列化,如下所示:

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)

任何帮助都深表感谢。

4

1 回答 1

1

堆栈跟踪表明类型解析中的无限递归Cls2扩展了它本身嵌套的类。这似乎是杰克逊的一个极端案例错误(报告它!)。同时,提取Cls2到一个独立的类而不是嵌套在它的超类中应该可以解决这个问题。

于 2012-08-07T16:56:00.300 回答