title: Serializing and deserializing Java generics using Jackson: StackOverflowError
body: <p>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.</p>
  
  <p>Here is my model:</p>
  
  <pre><code>@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.WRAPPER_OBJECT, property = "type")
  @JsonSubTypes(
                { 
                    @Type(value = Cls2.class, name = "Cls2") 
                })
  abstract class Cls1&lt;T&gt; implements Serializable
  {
      private T myObj;
  
      public T getMyObj()
      {
          return myObj;
      }
  
      public Cls1(T obj)
      {
          myObj = obj;
      }
      @JsonTypeName("Cls2")
      public static class Cls2&lt;E extends Int1&gt; extends Cls1&lt;E&gt; implements Serializable
      {
          public Cls2()
          {
              super(null);
          }
      }
  }
  
  @JsonTypeName("ChildContainer")
  class ChildContainer extends ParentContainer&lt;OtherBean&gt;
  {
  
  }
  
  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.WRAPPER_OBJECT, property = "type")
  @JsonSubTypes(
                { 
                    @Type(value = ChildContainer.class, name = "ChildContainer") 
                })
  class ParentContainer&lt;T extends RootBean&gt; 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
  {
  
  }
  </code></pre>
  
  <p>My goal is to serialize and deserialze using jackson as follows:</p>
  
  <pre><code>public static void main(String[] args) throws Exception
      {
  
          Cls2&lt;ChildContainer&gt; req = new Cls2&lt;ChildContainer&gt;();
  
          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
      }
  </code></pre>
  
  <p>I get the following java.lang.StackOverflowError during the serialization:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Any help is deeply appreciated.</p>