我正在创建一个类,我试图将类Bookstore的对象和Books列表添加到对象列表中。但是,我在添加书籍列表时遇到了类型错误。
这是我的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);
}
这是我的课程,它正在添加Bookstore对象和书籍列表:
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_XML)
public ResponseList addObjects() {
BookStore bookstore = new BookStore();
bookstore.setName("Prateek Bookstore");
bookstore.setLocation("Vasanth Nagar");
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);
return books;
}
这是错误:
Exception in thread "main" java.lang.ClassCastException:Book cannot be cast to java.util.ArrayList
这是我的ResponseList:
@XmlRootElement
@XmlSeeAlso({BookStore.class,Book.class,Hello.class})
public class ResponseList {
private List<Object> list;
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
}
这是BookStore类:
@XmlRootElement
@XmlType(propOrder = {"name", "location"})
public class BookStore {
private String name;
private String location;
public String getName() {
return name;
}
public String getLocation() {
return location;
}
public void setName(String name) {
this.name = name;
}
public void setLocation(String location) {
this.location = location;
}
}
这是Book类:
@XmlRootElement
public class Book {
private String name;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
云有人告诉我我做错了吗?
答案 0 :(得分:1)
查看测试类的最后一行:
ArrayList<Book> lb = (ArrayList<Book>) responseList.getList().get(1);
如果您评估
responseList.getList().get(1);
我想你会发现这会返回Book的一个实例。您正试图将其转换为ArrayList。
如果查看addObjects方法:
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);
添加书店后,将“Blist”的所有项添加到ArrayList中,因此该ArrayList的第二个元素是Book。
尝试将测试类的最后一行更改为
Book book1 = (Book) responseList.getList().get(1);
Book book2 = (Book) responseList.getList().get(2);
答案 1 :(得分:1)
在处理休息时,您必须使用Collection<Object>
代替ArrayList<Object>
。您总是可以尝试创建一个通用方法,例如:
编辑:nvm阅读下文。
您将Bookstore
对象放入arrayList
,然后放入book
对象。将它们放入arrayList
。
EDIT2:将主要的第2行更改为:
ArrayList<BookStore> bsList = (BookStore) responseList.getList().get(0);
BookStore bs = bsList.get(0);