users@jersey.java.net

[Jersey] Re: Using GenericType in a generic method

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Thu, 04 Aug 2011 11:43:09 +0200

Hmm, I saw similar issue some time ago and we resolved it like that:

       public <T> List<T> findAll(final Class<T> clazz) throws
UniformInterfaceException {
           ParameterizedType genericType = new ParameterizedType() {
               @Override
               public Type[] getActualTypeArguments() {
                   return new Type[] {clazz};
               }

               @Override
               public Type getRawType() {
                   return List.class;
               }

               @Override
               public Type getOwnerType() {
                   return List.class;
               }
           };

           GenericType<List<T>> type = new GenericType<List< T
>>(genericType) {};
           return
webResource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(type);
       }

so I guess you'll need to add Class<T> argument and you should be able
to use that..

hope it helps..

Regards,
Pavel

On 8/4/11 1:37 AM, dskyberg wrote:
> I have a method that, when using a normal PoJo works just fine. But
> converting to a generic method, the call to response.getEntity(new
> GenericType&lt;List&lt;T&gt;>(){}) throws the ever popular
> ClientHandlerExcpetion:
>
> A message body reader for Java class java.util.List, and Java type
> java.util.List<T>, and MIME media type application/json was not found
>
> Here's my code.
>
> public<T> List<T> getObjects(T t, String path){
> WebResource r = client.resource(props.props.getResourceServerPath());
>
> // Get JAXB response
> ClientResponse response = r.path(path)
> .header(HttpHeaders.AUTHORIZATION, accessTokenAsHeader())
> .accept(MediaType.APPLICATION_JSON)
> .get(ClientResponse.class);
> if(response.getClientResponseStatus() == ClientResponse.Status.OK){
> List<T> list = response.getEntity(new GenericType&lt;List&lt;T&gt;>(){});
>
> StringBuilder sb = new StringBuilder();
> sb.append(String.format("\tNo. of %1$ss: %2#s", t.getClass().getName(),
> list.size()));
> logger.info(sb.toString());
> return list;
> }
> else{
> ResourceServerError error =
> response.getEntity(ResourceServerError.class);
> logger.info(String.format("Resource Server returned error: %1#s - %2#s",
>
> error.getStatus(), error.getMessage()));
> return null;
> }
> }
>
> While the above code throws an exception, the following works just fine:
>
> ClientResponse response = r.path(path)
> .header(HttpHeaders.AUTHORIZATION, accessTokenAsHeader())
> .accept(MediaType.APPLICATION_JSON)
> .get(ClientResponse.class);
> if(response.getClientResponseStatus() == ClientResponse.Status.OK){
> List<Account> list = response.getEntity(new
> GenericType&lt;List&lt;Account&gt;>(){});
>
> StringBuilder sb = new StringBuilder();
> sb.append(String.format("\tNo. of %1$ss: %2#s", t.getClass().getName(),
> list.size()));
> logger.info(sb.toString());
> return list;
> }
>
> Getting this working will provide a significant reduction in code complexity
> and replication. So I hope someone has a nifty trick I can use!
>
> TIA
>
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Using-GenericType-in-a-generic-method-tp6650758p6650758.html
> Sent from the Jersey mailing list archive at Nabble.com.
>