users@jersey.java.net

Re: [Jersey] https://jersey.dev.java.net/issues/show_bug.cgi?id=18

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 22 Jan 2009 12:58:05 +0100

Hi Roman,

First i recommend upgrading to 1.0 or 1.0.1.

You need to wrap the collection around GenericEntity [1]. The reason
is because generic type information is erased when using the instance.
So you need to do the following:

   Collection<SystemUserDTO> usersList;
   GenericEntity<Collection<SystemUserDTO>> ge = new
GenericEntity<Collection<SystemUserDTO>>(usersList) {};
   return Response.ok().entity(ge).build();

I know this is not very nice, but we are running up against a
restriction in the Java language. Alternatively for 1.0.1 you can use
arrays:

   return Response.ok(usersList.toArray(new
JAXBXmlRootElement[usersList.size()])).build();


The choice on whether to return Response or return the response entity
should be based on whether to need to supply additional meta-data
(HTTP headers) with or without an entity. For example you can return
redirection responses or not modified responses, or responses with an
etag, last modified and an entity etc.

Paul.

[1] https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/core/GenericEntity.html

On Jan 22, 2009, at 12:43 PM, coldserenity wrote:

>
> Hello Paul,
>
>
> Paul Sandoz wrote:
>>
>> I have just committed basic support in the trunk.
>>
>> You can return any type that is a instance of Collection<T> where T
>> is
>> a JAXB bean annotated with @XmlRootElement. The root element name is
>> derived from the class name of T and is pluralized (assuming the name
>> is an English word).
>>
>
> I'm using Jersey 1.0-ea.
> This indeed works when Collection<T> is a return type of the WS method
> E.g.
> @GET
> @Path("{start}/{count}")
> public Collection<SystemUserDTO> getUsers(@PathParam("start")
> Integer
> start, @PathParam("count") Integer count);
>
> however when I try to return a collection in the following manner
> @GET
> @Path("{start}/{count}")
> public Response getUsers(@PathParam("start") Integer start,
> @PathParam("count") Integer count) {
> Collection<SystemUserDTO> usersList;
> ....
> return Response.ok().entity(usersList).build();
> }
> I receive following exception:
> com.sun.jersey.api.client.UniformInterfaceException: Status: 500
> at com.sun.jersey.api.client.WebResource.handle(WebResource.java:468)
> at com.sun.jersey.api.client.WebResource.access
> $200(WebResource.java:64)
> at com.sun.jersey.api.client.WebResource
> $Builder.get(WebResource.java:356)
> ...
> Is this a correct behavior or a bug with
> Response.ok().entity(Collection<T>)? I couldn't find more on this
> than your
> post.
>
> Also on more question not directly related to Jersey. Which way of
> returning
> results is more proper, the first one with explicit data type, or
> the second
> one where everything is wrapped into response?
>
> From what I see, first way is more strict, that is or you return a
> result or
> you throw an exception. While the second approach gives you freedom
> in terms
> of what to return.
> What's your opinion?
>
> Roman
>
> --
> View this message in context: http://n2.nabble.com/Re%3A-https%3A--jersey.dev.java.net-issues-show_bug.cgi-id%3D18-tp1082350p2197279.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
>