在Web服务中返回java.util.List

时间:2011-08-17 18:58:31

标签: java web-services client-server axis2 xmlbeans

我尝试在eclipse中创建一个Bottom up Java bean Web Service,我需要返回一个java.util.List所以我在服务器上有这个

public class BookStoreWS {
    private Book book1;
    private List<Book> listOfBooks;

    public Book getBook(){
        return  book1;
    }
    public List<Book> getBooks(){
        return  listOfBooks;
    }
}

并在客户端上

public static void main(String[] args) {
    BookStoreWSStub stub = new BookStoreWSStub();
    GetBookResponseDocument gbrd = stub.getBook();
    Book book = gbrd.getGetBookResponse().getReturn();//OK

    GetBooksResponseDocument gbrd2 = stub.getBooks();
    XmlObject OMGIAMNOTALIST = gbrd2.getGetBooksResponse().getReturn();//BAD
}

所以getBook()会返回我想要的对象Book,但getBooks()会返回XmlObject而不是List<Book>

获取List<Book>代替XmlObject的任何简单方法?或者将XmlObject转换为List<Book>

1 个答案:

答案 0 :(得分:0)

这就是你要找的东西吗?

List<A> OMGIAMALIST = new ArrayList<A>();
OMGIAMALIST.addAll(stub.getAs());

for ( A item : OMGIAMALIST ) {
    XmlObject OMGIAMANITEM = item.getGetAsResponse().getReturn();
}

XmlObject(最有可能)不能是A,但肯定是。