users@jersey.java.net

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

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 21 Apr 2009 12:54:32 +0200

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.

>
> James
>
> On Tue, Apr 21, 2009 at 11:08 AM, James Allchin <james_at_knowledgemill.com
> > wrote:
> Hi,
>
> Thanks for the great response on my last post.
>
> This time I am having difficulty dealing with arrays.
>
> In my previous post I was having problems with returning a custom
> Object (with JAXB annotations).
>
> I am now having difficulties generating the XML for an Array of
> these Objects.
>
> My example class is called Artifact:
>
> import javax.xml.bind.annotation.*;
>
> /**
> *
> * @author km
> */
> @javax.xml.bind.annotation.XmlRootElement
> @javax.xml.bind.annotation.XmlAccessorType(XmlAccessType.FIELD)
> public class Artifact {
>
> //_at_javax.xml.bind.annotation.XmlElement
> protected long artifactId;
>
> //_at_javax.xml.bind.annotation.XmlElement
> protected String artifactType;
>
> public Artifact() {
> }
>
> /**
> * @return the artifactId
> */
> public long getArtifactId() {
> return artifactId;
> }
>
> /**
> * @param artifactId the artifactId to set
> */
> public void setArtifactId(long artifactId) {
> this.artifactId = artifactId;
> }
>
> /**
> * @return the artifactType
> */
> public String getArtifactType() {
> return artifactType;
> }
>
> /**
> * @param artifactType the artifactType to set
> */
> public void setArtifactType(String artifactType) {
> this.artifactType = artifactType;
> }
>
> }
>
> This class is all good - works well when marshalling and
> serlializing to XML.
>
> An example of the method that returns the XML for this object is
> shown below:
>
> @GET
> @Path("/user/{userId}/artifacts/{artifactId}")
> //_at_Produces("application/json")
> public Artifact getArtifact (@PathParam("userId") long userId
> , @PathParam("artifactId") long
> artifactId) {
> Artifact returnedArtifact = null;
> try {
> returnedArtifact = dA.getArtifact(0, userId, artifactId);
> }
> catch (Exception e) {
> e.printStackTrace();
> }
> return returnedArtifact;
> }
>
> This works great.
>
> However, when I introduce a new method which returns an Array of
> Artifact objects. I get problems:
>
> @GET
> @Path("/user/{userId}/artifacts")
> public Artifact[] getArtifacts (@PathParam("userId") long
> userId) {
> Artifact[] returnedArtifacts = null;
> try {
> System.out.println("userId value is " + userId);
> returnedArtifacts = dA.getArtifacts(1, userId);
> }
> catch (Exception e) {
> e.printStackTrace();
> }
> return returnedArtifacts;
> }
>
>
> In this case I get the following error:
>
> SEVERE: A message body writer for Java type, class
> [Lcom.knowledgemill.entities.Artifact;, and MIME media type, text/
> xml, was not found
> javax.ws.rs.WebApplicationException
> at
> com
> .sun
> .jersey.spi.container.ContainerResponse.write(ContainerResponse.java:
> 239)
> at
> com
> .sun
> .jersey
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:752)
> etc.. etc...
>
> Basically, my question is - How do I serlialize and array of the
> custom objects.
>
> Perhaps I have to use a collection instead (this would be
> disappointing - arrays seem neater and more efficient).
>
> Thanks for any help.
>
> Regards,
>
> James
>