获取错误未找到 Java 类 java.util.ArrayList/List<java.lang.String> 的消息正文编写器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17342218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting error A message body writer for Java class java.util.ArrayList/List<java.lang.String> was not found
提问by user2416728
well this has been posted a lot of time here but no solution worked for me...
i can avoid this error by making a wrapper class but it only returned
好吧,这已经在这里发布了很多时间,但没有解决方案对我有用......
我可以通过创建一个包装类来避免这个错误,但它只返回
</stringWrapper>
</stringWrapper>
what am i doing wrong ?
我究竟做错了什么 ?
StringWrapper class:
StringWrapper 类:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class StringWrapper {
public StringWrapper (){}
List<String> list=new ArrayList<String>();
public void add(String s){
list.add(s);
}
}
code :
代码 :
@Path("/xml")
@GET
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public StringWrapper mystring(){
StringWrapper thestring=new StringWrapper();
thestring.add("a");
thestring.add("a");
return thestring;
}
Java Rest webservice using Jersey.
使用 Jersey 的 Java Rest 网络服务。
回答by Alex Pakka
You should add at least a getter in your StringWrapper. Without public attributes, there is nothing to serialize. So the output is correct. If you do not want a tag around the list of your strings, you can mark a single getter with @XmlValue.
您应该在 StringWrapper 中至少添加一个 getter。没有公共属性,就没有什么可序列化的。所以输出是正确的。如果您不想在字符串列表周围添加标签,则可以使用 @XmlValue 标记单个 getter。
@XmlRootElement
public class StringWrapper {
public StringWrapper (){}
List<String> list=new ArrayList<String>();
public void add(String s) { list.add(s); }
@XmlValue
public List<String> getData() { return list; }
}