users@jersey.java.net

Re: [Jersey] Jersey Client JAXB Collection

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 09 Jan 2009 11:18:22 +0100

HI Stephen,

Note that you can also do this:

   Application app = c.resource(RESOURCE_URI + "/ids/
1").accept("application/XML").get(Application.class);

And if you want to build the URI safely you can do:

   URI u = UriBuilder.fromUri(RESOURCE_URI).path("/ids/1").build();

There is a JAXB sample here that shows how to use collections:

   http://download.java.net/maven/2/com/sun/jersey/samples/jaxb/1.0.1/jaxb-1.0.1-project.zip

where you can do this:

         GenericType<Collection<Application>> genericApplication =
                 new GenericType<Collection< Application >>() {};

         Collection< Application > ce1 = c.resource(RESOURCE_URI + "/
ids/1").accept("application/XML")
                 get(genericApplication);

The reason for the above is that generic type information is lost at
runtime so we need to wrap that information in something, GenericType,
so that it is not lost.

For 1.0.2, which will be released at end of Jan or in Feb arrays are
supported, which IMHO are easier to use:

   http://download.java.net/maven/2/com/sun/jersey/samples/jaxb/1.0.2-SNAPSHOT/jaxb-1.0.2-SNAPSHOT-project.zip

So you can do this:

   Application[] app = c.resource(RESOURCE_URI + "/ids/
1").accept("application/XML").get(Application[].class);

Paul.


On Jan 8, 2009, at 7:35 PM, Lawson, Stephen R. -ND wrote:

> I have my classes annotated with JAXB, and I am able to retrieve
> individual objects and have them unmarshalled using the Jersey
> client using the code below. However, I have not figured out how to
> retrieve a collection of objects using the Jersey client. Can
> someone provide code that I would use to unmarshal a collection into
> JAXB objects? Thank you.
>
> Client c = Client.create();
> ClientResponse response = c.resource(RESOURCE_URI + "/ids/
> 1").accept("application/XML").get(ClientResponse.class);
> Application app = response.getEntity(Application.class);
>