gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:23 26 4
gpt4 key购买 nike

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

我的代码示例:

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

}

上面给了我一个异常(exception):

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 - RESTEasy - 简单的字符串数组/集合编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2871972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com