编程语言
首页 > 编程语言> > java-RESTEasy-简单的字符串数组/集合编组

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

}

标签:jax-rs,jboss,resteasy,java
来源: https://codeday.me/bug/20191024/1917655.html