出现错误 Java 类 java.util.ArrayList/List<java.lang.String> 的消息正文编写器没有找到

2022-01-21 00:00:00 rest web-services java jersey

好吧,这已经在这里发布了很多时间,但没有对我有用的解决方案...
我可以通过创建一个包装类来避免这个错误,但它只返回了

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 类:

StringWrapper class:

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

代码:

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

使用 Jersey 的 Java Rest web 服务.

Java Rest webservice using Jersey.

推荐答案

你应该在你的 StringWrapper 中添加至少一个 getter.没有公共属性,就没有什么可序列化的.所以输出是正确的.如果您不想在字符串列表周围添加标签,可以使用 @XmlValue 标记单个 getter.

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.

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

}

相关文章