title: Weblogic 12.2.1.3.0 REST service with sub-resource and generics
body: <p>I have the following simple classes for my REST application:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class BaseDTO {
      private long id;
      private String name;
  
      // setters and getters
  }
  
  @XmlRootElement
  public class SimpleDTO extends BaseDTO {
      private String description;
  
      // setters and getters
  }
  
  public class SimplePojo extends BasePojo {
      private String description;
  
      public abstract class BaseResource&lt;DTO, POJO&gt; {
  
          @POST
          @Consumes(MediaType.APPLICATION_JSON)
          @Produces(MediaType.APPLICATION_JSON)
          public Response create(DTO payload) {
              POJO pojo = this.getPojoFromDto(payload);
              return Response.ok(pojo).build();
          }
  
      public abstract POJO getPojoFromDto(DTO dto);
  }
  
  @ApplicationScoped
  public class SimpleResource extends BaseResource&lt;SimpleDTO, SimplePojo&gt; {
  
      @Override
      public SimplePojo getPojoFromDto(SimpleDTO dto) {
          SimplePojo pojo = new SimplePojo();
          pojo.setId(dto.getId());
          pojo.setName(dto.getName());
          pojo.setDescription(dto.getDescription());
  
          return pojo;
      }
  }
  
  @Dependent
  @Path("/models")
  public class SimpleModelResource {
  
      @Inject
      private Instance&lt;Object&gt; subResource;
  
      @Path("/{model-id:[0-9]+}")
      public SimpleRouterResource getModel() {
          return subResource.select(SimpleRouterResource.class).get();
      }
  }
  
  @ApplicationPath("/api")
  public class RestApplication extends Application {
  
      @Override
      public Set&lt;Class&lt;?&gt;&gt; getClasses() {
          Set&lt;Class&lt;?&gt;&gt; classes = new HashSet&lt;&gt;();
          classes.add(SimpleModelResource.class);
          return classes;
      }
  }
  </code></pre>
  
  <h2>WildFly 8.2.1.Final OK</h2>
  
  <p>If I deploy the application under WildFly 8.2.1.Final everything works fine, I can shot the post request <code>localhost:8080/web-app/api/models/9/simple</code> and the method <code>getPojoFromDto</code> is called, the POJO is created and returned.</p>
  
  <p>WildFly uses Jackson 2.4.1.</p>
  
  <h2>Weblogic 12.2.1.3.0 KO</h2>
  
  <p>If I deploy under Weblogic 12c it does not work, it produces a <code>ClassCastException</code>.</p>
  
  <pre><code>]] Root cause of ServletException.
  java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to 
  com.dummyprojects.model.SimpleDTO
      at 
  com.dummyprojects.rest.SimpleResource.getPojoFromDto(SimpleResource.java:8)
      at com.dummyprojects.rest.BaseResource.create(BaseResource.java:19)
      at   
   com.dummyprojects.rest.SimpleResource$Proxy$_$$_WeldClientProxy.create(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at 
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  </code></pre>
  
  <p><strong>Note!</strong> I have disable MOXy in favor or Jackson, the one provided by Weblogic so version 2.4.5.</p>
  
  <h3>What I have tried</h3>
  
  <ul>
  <li>put in the <code>WEB-INF\lib</code> Jackson 2.9.2 and reference from <code>weblogic.xml</code></li>
  </ul>
  
  <p>I guess the issue is something related to <code>jersey</code> but I am not sure.</p>
  
  <p><a href="https://i.stack.imgur.com/ZDVW3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZDVW3.jpg" alt="enter image description here"></a></p>
  
  <p>Do you have any suggestion or already found this problem?</p>
  
  <p>Thanks a lot in advance</p>
  
  <h3>Update 1</h3>
  
  <p>I have added some restrictions</p>
  
  <pre><code>public abstract class BaseResource&lt;DTO extends BaseDTO, POJO extends BasePojo&gt; {...}
  </code></pre>
  
  <p>the exception now, as I expected, is</p>
  
  <pre><code>]] Root cause of ServletException.
  java.lang.ClassCastException: com.dummyprojects.model.BaseDTO cannot be cast 
  to com.dummyprojects.model.SimpleDTO
  </code></pre>
  
  <h3>Update 2 (Solution)</h3>
  
  <pre><code>public abstract class BaseResource&lt;DTO extends BaseDTO, POJO extends BasePojo&gt; {
  
      protected final Class&lt;DTO&gt; dtoType; // resolved in the constructor
  
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      public Response create(InputStream payload, @Context Providers providers) throws IOException {
          DTO simpleDTO = (DTO)providers.getMessageBodyReader(dtoType, dtoType, new Annotation[0], MediaType.APPLICATION_JSON_TYPE)
              .readFrom(dtoType, dtoType, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MultivaluedHashMap&lt;&gt;(), payload);
  
  
          POJO pojo = this.getPojoFromDto(simpleDTO);
          return Response.ok(pojo).build();
      }
  
      public abstract POJO getPojoFromDto(DTO dto);
  }
  </code></pre>
  
  <p>So the issue is that the object injected by <code>weld</code> has still the basic type <code>BaseDTO</code> instead of the type of the subclass <code>SimpleDTO</code>. I suspect an issue in <code>jersey</code> or <code>weld</code>.</p>
  
  <p>The workaround is to pass the good type to <code>Jackson</code> to get the <code>JSON</code> correctly unmarshalled.</p>
  
  <p>I hope it helps.</p>