Quantcast

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

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

JavaGeek_Boston
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Paul Sandoz
Administrator
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: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

JavaGeek_Boston
Thank you, that worked perfectly.


Paul Sandoz wrote
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@jersey.dev.java.net
> For additional commands, e-mail: users-help@jersey.dev.java.net
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@jersey.dev.java.net
For additional commands, e-mail: users-help@jersey.dev.java.net
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

pulkitsinghal
This post has NOT been accepted by the mailing list yet.
In reply to this post by Paul Sandoz
The following syntax is incorrect as this constructor is protected in Jersey now:
GenericType<Collection<Company>> genericType = new GenericType<Collection<Company>>();

Can you give an example with the use of the newer constructor? Please :)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

pulkitsinghal
This post has NOT been accepted by the mailing list yet.
Nevermind, just realized its supposed to be an anon subclass and I'm missing the {}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Chuck Zheng
This post has NOT been accepted by the mailing list yet.
In reply to this post by Paul Sandoz
Is your service impl just return List<Token> as below?

> @GET
> @Path("tokens")
> public List<Token> foo(){
>   //...return an ArrayList<Token>.
> }

I saw some other posts mentioning GenericEntity, not sure that is needed:

http://aruld.info/handling-generified-collections-in-jersey-jax-rs/
Loading...