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.
Thanks
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
>