Dropwizard deserializing generic list from JerseyClient

前端 未结 2 1810
执笔经年
执笔经年 2021-01-14 12:12

I wanted to implement a generic class to use for caching results from a REST API in a local MongoDB-instance. For this to work, I need to deserialize a collection I get from

相关标签:
2条回答
  • 2021-01-14 12:42

    You can use

    import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
    import javax.ws.rs.core.GenericType;
    
     GenericType<List<T>> genericType = new GenericType<>( 
            ParameterizedTypeImpl.make( List.class, new Type[]{classType}, null));
    
    0 讨论(0)
  • 2021-01-14 12:50

    Java's most popular excuse for Generics: Type Erasure

    If you can pass your class type as Class<T> clazz, then you can use this:

    GenericType<List<T>> genericType = new GenericType<>(new ParameterizedType() {
      public Type[] getActualTypeArguments() {
        return new Type[]{clazz};
      }
    
      public Type getRawType() {
        return List.class;
      }
    
      public Type getOwnerType() {
        return null;
      }
    });
    response.readEntity(genericType);
    
    0 讨论(0)
提交回复
热议问题