users@jersey.java.net

[Jersey] return Array of Primitives

From: Knese, Karsten <karsten.knese_at_kit.edu>
Date: Tue, 22 Feb 2011 10:09:02 +0100

Hey Folks.

Currently I am working on a restful webservice and want to return a array of primitives, e.g. a simple string-array.
Unfortunately I am not able to fulfill this task. I tried several methods:

Simple Method:
@GET
        @Path("stringarray")
        @Produces(MediaType.APPLICATION_JSON)
        public String[] test(){
                return new String[]{"one","two","three"};
        }


Return List:
        @GET
        @Path("stringarray")
        @Produces(MediaType.APPLICATION_JSON)
        public List<String> blub(){
                
                ArrayList<String> list = new ArrayList<String>();
                list.add("one");
                list.add("two");
                list.add("three");
                GenericEntity<List<String>> entity = new GenericEntity<List<String>>(list){};
                return entity.getEntity();
        }


What I expect is the following:
<strings>
     <string>one</string>
     <string>two</string>
     <string>three</string>
</strings>


What I get:
Feb 21, 2011 4:59:21 PM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<java.lang.String>, and MIME media type text/xml was not found
        at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1316)
        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1229)
        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1219)
        at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:419)



But now one things works fine, when I encapsulate the array in a PoJo:

import java.util.ArrayList;
import java.util.Collection;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Car {

        public Car() {
        }
        
        @XmlElement
        private String id;
        @XmlElement
        private int counter;
        @XmlElement
        @XmlElementWrapper(name="StringArray")
        private Collection<String> array = new ArrayList<String>();

        public Car(String id) {
                this.id = id;
                counter++;
                array.add("one");
                array.add("two");
        }
        
        public void add(){
                counter++;
        }
        
        public int getCounter(){
                return counter;
        }

        public String getSession() {
                return id;
        }

}



<cars>

<car>
<id>one</id>
<counter>1</counter>

<StringArray>
<array>one</array>
<array>two</array>
</StringArray>
</car>

<car>
<id>two</id>
<counter>1</counter>

<StringArray>
<array>one</array>
<array>two</array>
</StringArray>
</car>

<car>
<id>three</id>
<counter>1</counter>

<StringArray>
<array>one</array>
<array>two</array>
</StringArray>
</car>
</cars>



What I am doing wrong? I really run out of ideas. Caused by the lack of this feature, I went on with x-stream. Which works quite fine, but that I have to add a new technology...

Cheers,
Karsten