4

我有一个返回对象数组列表的类 BookMain。我正在使用 REST 服务获取输出,但出现错误。

这是我的 BookMain 课程:

@GET
@Path("/get")
@Produces(MediaType.APPLICATION_XML)
public ArrayList<Object> addObjects() {

    Book book = new Book(); 
    book.setName("The Book");
    book.setAuthor("Author");

    BookStore bookstore = new BookStore();
    bookstore.setName("The Bookstore");
    bookstore.setLocation("UK");

    ArrayList<Object> list = new ArrayList<Object>();
    list.add(book);
    list.add(bookstore);

    return list;   
}

这是我得到的错误:

11 Jul, 2013 3:36:52 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class java.util.ArrayList, and Java type java.util.ArrayList<java.lang.Object>, and MIME media type application/xml was not found 11 Jul, 2013 3:36:52 PM com.sun.jersey.spi.container.
ContainerResponse write SEVERE: The registered message body writers 
compatible with the MIME media type are:application/xml ->
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App

任何人都可以为我提供解决方案吗?

4

3 回答 3

8

引入一个新的类如下

@XmlRootElement(name = "responseList")
public class ResposeList {

    private List<Object> list;

    public List<Object> getList() {
        return list;
    }

    public void setList(List<Object> list) {
        this.list = list;
    }

}

并将列表设置如下

@GET
@Path("/get")
@Produces(MediaType.APPLICATION_XML)
public ResposeList addObjects() {

    Book book = new Book(); 
    book.setName("Here is the Game");
    book.setAuthor("HHH");

    BookStore bookstore = new BookStore();
    bookstore.setName("Prateek Bookstore");
    bookstore.setLocation("Vasanth Nagar");

    ArrayList<Object> list = new ArrayList<Object>();
    list.add(book);
    list.add(bookstore);
    ResposeList books=new ResposeList();
    books.setList(list);

    return books;   
}
于 2013-07-11T10:39:14.810 回答
3

您需要将实体(Arraylist)包装在 Response 对象下。此外,您的休息方法返回类型应该是响应。这是您需要做的:

@GET
@Path("/get")
@Produces(MediaType.APPLICATION_XML)
public Response addObjects() {

    Book book = new Book(); 
    book.setName("Here is the Game");
    book.setAuthor("HHH");

    BookStore bookstore = new BookStore();
    bookstore.setName("Prateek Bookstore");
    bookstore.setLocation("Vasanth Nagar");

    ArrayList<Object> list = new ArrayList<Object>();
    list.add(book);
    list.add(bookstore);

    return Response.status(200).entity(list).build();
}

@XmlElemen还要在 ResponseList bean 的 getter 上添加t

@XmlRootElement
public class ResponseList {

private ArrayList<Object> list;

@XMLElement("booksList")
public ArrayList<Object> getList() {
    return list;
}

public void setList(ArrayList<Object> list) {
    this.list = list;
}   
}
于 2013-07-11T10:15:56.873 回答
0

您可以简单地使用 JSONArray 来显示结果。创建一个 JSONArray 对象并将列表传递给 JSONArray() 构造函数。然后以字符串格式返回 JSONArray 对象。将您的方法的返回类型更改为 String 并且以下代码将起作用。

块引用

JSONArray a=new JSONArray(list); 返回 a.toString();

于 2017-06-06T10:31:16.083 回答