tags:

views:

342

answers:

3

Is there a simple way for marshalling and unmarshalling String[] or List in RESTEasy?

My code sample :

@GET
@Path("/getSomething")
@Produces(MediaType.APPLICATION_JSON)
public List<String> getSomeData() {
    return Arrays.asList("a","b","c","d");

}

Above gives me an Exception :

Could not find MessageBodyWriter for response object 
of type: java.util.Arrays$ArrayList of media type: application/json
A: 

Are you using Resteasy 1.2.1.GA? I've done this with XML (but not JSON).

Otherwise you may need to make a wrapper.

Robert Wilson
A: 

I have the same problem with both XML and JSON. Have not found a solution for it yet but I think it has to do with JAXB.

So it turns out that the problem is that JAXB already comes with JDK6 and the dependencies from JBoss is incorrect. They should find another solution for it that how it is done now. Any how this is how you can solve it:

<!-- JAXB Reasteasy support -->
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxb-provider</artifactId>
<version>1.2.1.GA</version>
<scope>compile</scope>
<exclusions>
    <exclusion>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
    </exclusion>
    <exclusion>
        <groupId>com.sun.xml.stream</groupId>
        <artifactId>sjsxp</artifactId>
    </exclusion>
</exclusions>

You will get the RESTEASY JAXB provider but not the JAXB files from maven.

Peter Eriksson
A: 

You might need to wrap it like this:

public List<JaxbString> getList(){
     List<JaxbString> ret= new ArrayList<JaxbString>();
     List<String> list = Array.asList("a","b","c");
          for(String s:list){
              ret.add(new JaxbString(s));
          }
     return ret;
}

@XmlRootElement
public class JaxbString {

    private String value;

    public JaxbString(){}

    public JaxbString(String v){
        this.setValue(v);
    }

    public void setValue(String value) {
        this.value = value;
    }

    @XmlElement
    public String getValue() {
        return value;
    }

}
Ben