users@jersey.java.net

Re: [Jersey] javax.xml.bind.JAXBException when building Response

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 04 Jan 2010 10:13:00 +0100

Hi,

You need to wrap the type around a GenericEntity instance to preserve
the type information:

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

Otherwise if you are just returning Response.ok(...).build() you can
declare the entity type as the return type of the resource method.

Note that Jersey will derive a root element name of the list from the
class of the item (currently it is not possible to configure this name).


On the client side you need to use:

   https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/client/GenericType.html

Note that we made a big mistake in the EG because we tried and failed
to make Response generic (but subsequently we now have worked out how
to do that).

Paul.

On Jan 4, 2010, at 6:05 AM, cgswtsu78 wrote:

>
> Hello,
>
> I'm relatively new to jaxrs, jersey and rest web service
> implementations in
> general so this is probably a very easy question to answer.
>
> I'm using jersey to return a collection of objects to the consumer
> of the
> service using the response.getEntity(classname) as depicted below.
> I have
> two classes ChartDataWrapper and EnvFromHourly, depicted below.
>
> My Question would be how can I return just a List of EnvFromhourly
> objects
> using the Response object? I get a write exception when I try
> Response.ok(ArrayList of EnvFromHourly objects).build(); I created
> ChartDataWrapper to hold a List of EnvFromHourly and then I do a
> Response.ok(ChartDataWrapper).build(); and that works, but I'm
> looking for a
> more efficient way
>
>
>
>
> EnvFromHourly:
> @XmlRootElement(name="EnvFromHourly")
> public class EnvFromHourly implements Serializable
> {
> //variables and getters/setters
> }
>
> EnvFromHourly:
> @XmlRootElement(name="ChartDataWrapper")
> public class ChartDataWrapper implements Serializable
> {
> private List<?> items;
>
> public List<?> getItems() {
> return items;
> }
> public void setItems(List<?> items) {
> this.items = items;
> }
> }
>
> Service Method:
> @POST
> @Path(TOP_SPAM_SENDER_DATA)
> @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
> public Response getTopSpamSenderData(TopSpamSenderChart chart) throws
> Exception {
>
> //getTopSpamSender just returns a populated ChartDataWrapper object
> return Response.ok(getTopSpamSender(chart)).build();
> }
>
> Service client code:
> Client client = Client.create();
> WebResource webResource = client.resource(ChartUtils.getURL() +
> BASE_RESOURCE_LOCATION);
> ClientResponse response =
> webResource
> .path
> (TOP_SPAM_SENDER_DATA
> ).type(MediaType.APPLICATION_XML).post(ClientResponse.class,
> chart);
>
> ChartDataWrapper wrapper =
> response.getEntity(ChartDataWrapper.class);
> --
> View this message in context: http://n2.nabble.com/javax-xml-bind-JAXBException-when-building-Response-tp4248580p4248580.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
>