users@jersey.java.net

Re: [Jersey] How can I parse a java.util.List<?>? Is it supported by the Jersey client?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 10 Feb 2009 11:43:49 +0100

Hi Steven,

This is not a stupid question. It is not so obvious when Java generics
is involved.

The main problem is one of type erasure. When you instantiate a
instance of List<Token> the generic type information is lost, and the
types List<Token> and List are not equal. So when you tell the Jersey
client API to return an instance of List using List.class there is not
enough information to declare that what you really want is an instance
of List<Token>.

There are two ways you can do this (if using Jersey 1.0.1):

1) Using arrays:

     Client c = ...
     Token[] ts = c.resource(...).path(...).get(Token[].class);

2) Using the the class com.sun.jersey.api.client.GenericType to ensure
that
      generic type information is retained:

     List<Token>[] ts = c.resource(...).path(...).get(new
GenericType<List<Token>>() {});

Hope this helps,
Paul.



On Feb 10, 2009, at 4:42 AM, JavaGeek_Boston wrote:

>
> Hello All,
> This is probably a stupid question, but how do I create services
> that return
> lists? I've created a web service
> @GET
> @Path("tokens")
> public List<Token> foo(){
> //...return an ArrayList<Token>.
> }
> It seems to work great from my browser. How can I parse it?
>
> com.sun.jersey.api.client.ClientHandlerException: A message body
> reader for
> Java type, class java.util.List, and MIME media type, application/
> xml, was
> not found
>
> I'm attempting to parse it with the following command:
> (new Client()).resource(SERVER_URL).path(MY_PATH).get(List.class);
>
> (new Client()).resource(SERVER_URL).path(MY_PATH).get(String.class);
> works,
> returning the expected XML, ruling out a path issue.
>
> Any help is greatly appreciated.
>
> Thanks in Advance,
> Steven
> Harvard Children's Hospital Informatics Program
> --
> View this message in context: http://n2.nabble.com/How-can-I-parse-a-java.util.List%3C-%3E---Is-it-supported-by-the-Jersey-client--tp2300852p2300852.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>