Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTEasy - simple string array/collection marshalling

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
like image 870
Piotr Gwiazda Avatar asked Apr 10 '26 05:04

Piotr Gwiazda


1 Answers

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;
    }

}
like image 93
Ben Avatar answered Apr 12 '26 21:04

Ben