users@jersey.java.net

Re: Re: [Jersey] Jersey JAX-RS JAXB - Returning an Array of Objects

From: Ari Heino <ext.ari.heino_at_tieto.com>
Date: Fri, 7 Aug 2009 04:13:37 -0500 (CDT)

Paul Sandoz wrote:
>
>
> On Apr 21, 2009, at 12:36 PM, James Allchin wrote:
>
>> Hi,
>>
>> Just as an update. I tried it with a collection (ArrayList):
>>
>> @GET
>> @Path("/user/{userId}/artifacts")
>> public ArrayList<Artifact> getArtifacts (@PathParam("userId")
>> long userId) {
>> ArrayList<Artifact> returnedArtifacts = null;
>> try {
>> System.out.println("userId value is " + userId);
>> // We use the getArtifacts method just passing in 1 id value
>> returnedArtifacts = dA.getArtifactsAsList(1, userId);
>> }
>> catch (Exception e) {
>> e.printStackTrace();
>> }
>> return returnedArtifacts;
>> }
>>
>> This works - however, it would be neater for me to use a Java Array
>> [] - or is this unsupported by JAXB.
>>
>
> It is supported in 1.0.2 and beyond, see:
>
> http://download.java.net/maven/2/com/sun/jersey/samples/jaxb/1.0.3/jaxb-1.0.3-project.zip
>
> http://fisheye4.atlassian.com/browse/~raw,r=1785/jersey/trunk/jersey/
> samples/jaxb/src/main/java/com/sun/jersey/samples/jaxb/
> JAXBArrayResource.java
>
> Paul.
>
>
Is it possible to return those collections in a response instead of
returning the collection itself as a return value of the method? To make use
of Response's status codes and such?

So instead of:

@GET
public List<String> getStuph(){
  return new ArrayList();
}

I could have:

@GET
public Response getStuph() {
  return Response.ok().entity(new ArrayList()).build();
}

I tried this with Jersey 1.0.3. and it complains:

7.8.2009 10:52:49 com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java type, class java.util.ArrayList, and
MIME media type, text/xml, was not found

I tried using @Produces( { MediaType.APPLICATION_XML }) and @Produces( {
MediaType.TEXT_XML }) with no difference to the error result.

-- .aRi

-- 
View this message in context: http://n2.nabble.com/Jersey-JAX-RS-JAXB---Returning-an-Array-of-Objects-tp2669077p3403180.html
Sent from the Jersey mailing list archive at Nabble.com.