我目前正在开发一个使用泛型的模型,并不复杂。我知道类似的问题已得到解答,但没有一个明确回答我的问题。
这是我的模特:
@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
{
}
我的目标是使用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
}
在序列化期间我得到以下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 :(得分:1)
堆栈跟踪建议在Cls2
的类型解析中进行无限递归,它扩展了它本身嵌套的类。这似乎是杰克逊(report it!)中的一个角落案例错误。同时,将Cls2
提取到独立类而不是将其嵌套在超类中应该可以解决这个问题。