录中,然后在控制台中输入 ant run。
客户端 JAX-WS 使用
清单 6 展示了完整的测试客户机代码。如果将其与 清单 2 比较,您将观察到未封装接口和已封装接口之间的不同,其中未封装接口具有更好的编程友好性。
清单 6. JAX-WS 测试客户机代码
public class WebServiceClient
{
public static void main(String[] args) throws Exception {
// check for required command line parameters
if (args.length < 3) {
System.out.println("Usage:\n java " +
"com.sosnoski.ws.library.jaxws.WebServiceClient host port path");
System.exit(1);
}
// create the client stub
JaxwsLibrary service = new JaxwsLibrary();
Library stub = service.getLibrary();
// set the actual endpoint address
String target = "http://" + args[0] + ":" + args[1] + args[2];
System.out.println("Connecting to " + target);
BindingProvider provider = (BindingProvider)stub;
provider.getRequestContext().
put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, target);
// retrieve a book directly
String isbn = "0061020052";
BookInformation book = stub.getBook(isbn);
if (book == null) {
System.out.println("No book found with ISBN ''" + isbn + ''\'''');
} else {
System.out.println("Retrieved ''" + book.getTitle() + ''\'''');
}
// retrieve the list of types defined
List<TypeInformation> types = stub.getTypes();
System.out.println("Retrieved " + types.size() + " types:");
for (int i = 0; i < types.size(); i++) {
TypeInformation type = types.get(i);
System.out.println(" ''" + type.getName() + "'' with " +
type.getCount() + " books");
}
// add a new book
String title = "The Dragon Never Sleeps";
isbn = "0445203498";
try {
List<String> authors = new ArrayList<String>();
authors.add("Cook, Glen");
stub.addBook("scifi", isbn, authors, title);
System.out.println("Added ''" + title + ''\'''');
title = "This Should Not Work";
stub.addBook("scifi", isbn, authors, title);
System.out.println("Added duplicate book - should not happen!");
} catch (AddDuplicateFault e) {
System.out.println("Failed adding ''" + title +
"'' with ISBN ''" + isbn + "'' - matches existing title ''" +
e.getFaultInfo().ge
|