2013-05-01 68 views
-1

如何在Java中的SOAP Web服务中将数组列表从服务器拉到客户端?从SOAP Web服务中提取列表

例子:

Server包含:书籍

客户的名单:要求所有书籍名单中,服务器的书籍应该返回到客户名单的响应。

我会得到的书籍列表中服务器的响应以及如何消耗客户端该列表?

+0

的https:// WWW。 google.com/search?q=java+and+soap+tutorial – 2013-05-01 12:36:13

+0

没用。他们是简单的教程不符合我的要求。 – 2013-05-01 12:40:52

+0

任何人都知道答案? – 2013-05-01 12:45:25

回答

0

不能使用列表,因为接口类型没有在Web服务通信允许(同时作为行动的返回类型和参数)。

您需要指定一个列表实现代替,如ArrayList。

还书切不可接口,并且所有的字段必须是未接口类型。您无法在任何地方传递任何接口类型的数据。

有没有遇到什么异常试图运行/部署web服务?如果是这样,为什么没有提到它呢?

如果你只是不知道如何编写代码,为什么你只是didnt尝试使用列表/ ArrayList中/ WhateverList定期型(如String)?

+0

“你不能使用列表,因为Web服务通信中不允许使用接口类型(既作为动作的返回类型,也作为参数)。”这不是真的。 – ACV 2016-09-01 10:38:22

1

简而言之:

服务:

package webservice; 
import java.util.ArrayList; 
import java.util.List; 
import javax.jws.WebMethod; 
import javax.jws.WebService; 
@WebService 
public class BookWebService { 
List<Book> allBooks = new ArrayList<>(); 
@WebMethod 
public List<Book> getAllBooks() { 
return allBooks; 
} 
@WebMethod 
public void addBook (Book book) { 
this.allBooks.add(book); 
} 
} 

客户端:

package root; 
import java.util.List; 
import javax.xml.ws.WebServiceRef; 
import webservice.Book; 
import webservice.BookWebService; 
import webservice.BookWebServiceService; 
public class MainClient { 
@WebServiceRef(wsdlLocation="http://localhost:8080/LibraryWs/BookWebServiceService?wsdl") 
private static BookWebServiceService service; 
public static void main(String[] args) { 
MainClient cl = new MainClient(); 
cl.start(); 
} 
public void start() { 
service = new BookWebServiceService(); 
BookWebService port = service.getBookWebServicePort(); 
System.out.println("All books: "+port.getAllBooks()); 
Book book = new Book(); 
book.setTitle("Journey"); 
book.setPages(233); 
port.addBook(book); 
System.out.println("All books: "+port.getAllBooks()); 
} 
} 

完整的具体步骤可在这里: JAX-WS Simple Client and Service