我正在尝试将对象BookStore和Book的对象列表添加到对象列表中。但我得到以下例外:
例外:
15 Jul, 2013 4:47:07 PM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.]
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:159)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
这是我的班级:
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_XML)
public ResponseList addObjects() {
BookStore bookstore = new BookStore();
bookstore.setName("Bookstore");
bookstore.setLocation("Book Palace");
Book book1 = new Book();
book1.setName("Book2");
book1.setAuthor("Author2");
Book book2 = new Book();
book2.setName("Book3");
book2.setAuthor("Author3");
ArrayList<Book> Blist = new ArrayList<Book>();
Blist.add(book1);
Blist.add(book2);
ArrayList<Object> list = new ArrayList<Object>();
list.add(bookstore);
list.addAll(Blist);
ResponseList books = new ResponseList();
books.setList(list);
System.out.println("returning books : " +books);
return books;
}
当我打印书籍对象时(见程序的最后一行),我得到以下输出:
returning books : ResponseList [list=[[Book [name=Book2, author=Author2], Book [name=Book3, author=Author3]]]]
但是,当我尝试使用其他客户端获取输出时,我收到错误。这是我的REST客户端:
public class Test {
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
ResponseList responseList = service.path("rest").path("BookMain/get").accept(MediaType.APPLICATION_XML).get(ResponseList.class);
BookStore bs = (BookStore) responseList.getList().get(0);
ArrayList<Book> lb = (ArrayList<Book>) responseList.getList().get(1);
}
请帮帮忙?
修改
这是我的ResponseList类:
@XmlRootElement
@XmlSeeAlso({BookStore.class,Book.class})
public class ResponseList {
private List<Object> list;
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
}