简体   繁体   English

使用Jackson序列化和反序列化Java泛型:StackOverflowError

[英]Serializing and deserializing Java generics using Jackson: StackOverflowError

I'm currently working on a model which uses generics and is little complicated. 我目前正在研究一个使用泛型且不太复杂的模型。 I understand that similar questions have been answered but none of them clearly answers mine. 我了解已经回答了类似的问题,但没有一个问题能明确回答我的问题。

Here is my model: 这是我的模型:

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

}

My goal is to serialize and deserialze using jackson as follows: 我的目标是使用jackson进行序列化和反序列化,如下所示:

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
    }

I get the following java.lang.StackOverflowError during the serialization: 我在序列化过程中收到以下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)

Any help is deeply appreciated. 任何帮助深表感谢。

The stacktrace suggests an infinite recursion in type resolving of Cls2 which extends the class it is by itself nested in. This seems to be a corner case bug in Jackson ( report it !). stacktrace建议在Cls2类型解析中进行无限递归,以扩展其本身嵌套的类。这似乎是Jackson的一个Cls2案例(请报告 !)。 In the meanwhile, extracting Cls2 into a standalone class instead of nesting it in its superclass should solve this problem. 同时,将Cls2提取到独立类中而不是将其嵌套在其超类中应该可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM