休息是否支持对象的arraylist?

时间:2013-07-11 10:09:21

标签: java rest object arraylist

我有一个类BookMain,它返回一个对象的arraylist。我正在使用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

有人可以为我提供解决方案吗?

3 个答案:

答案 0 :(得分: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;   
}

答案 1 :(得分:2)

您需要将您的实体(Arraylist)包装在Response对象下。你的rest方法返回类型也应该是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();
}

还在ResponseList bean的getter顶部添加@XmlElemen 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;
}   
}

答案 2 :(得分:0)

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

  

块引用

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