java-RESTEasy-简单的字符串数组/集合编组

在RESTEasy中是否有简单的方法来编组和解组String []或List?

我的代码示例:

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

}

上面给了我一个例外:

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

解决方法:

您可能需要像这样包装它:

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

}
上一篇:Java-在EJB 3.1中,容器代理在技术上如何有效地欺骗我的无接口对象?


下一篇:Java-石英-ClassCastException