
WebService用于程序服务. 仅在浏览器中访问WebService没有意义. 因此,在本节中web调用客户端程序,将使用Java实现控制台程序,以调用上一节中发布的WebService. 调用WebService的客户端代码如下:
-> packageclient;
importjavax.xml.namespace.QName;
importorg.apache.axis2.addressing.EndpointReference;
importorg.apache.axis2.client.Options;
importorg.apache.axis2.rpc.client.RPCServiceClient;
publicclassRPCClient
{
publicstaticvoidmain(字符串[] args)throwsException

{
//使用RPC方法调用WebService
RPCServiceClientserviceClient = newRPCServiceClient();
Optionsoptions = serviceClient.getOptions();
//指定用于调用WebService的URL
EndpointReferencetargetEPR = newEndpointReference(
“: 8080 / axis2 /服务/ SimpleService”);
options.setTo(targetEPR);
//指定getGreeting方法的参数值

对象[] opAddEntryArgs = newObject [] {“超人”};
//类对象,用于指定getGreeting方法的返回值的数据类型
Class [] classes = newClass [] {String.class};
//指定要调用的getGreeting方法和WSDL文件的名称空间
QNameopAddEntry = newQName(“”,“ getGreeting”);
//调用getGreeting方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,类)[0]);
//以下是调用getPrice方法的代码,类似于调用getGreeting方法的代码
classes = newClass [] {int.class};

opAddEntry = newQName(“”web调用客户端程序,“ getPrice”);
System.out.println(serviceClient.invokeBlocking(opAddEntry,newObject [] {},类)[0]);
}
}
运行上述程序后,将在控制台上输出以下信息:
编写客户端代码时,请注意以下几点:
1. 客户端代码需要引用许多Axis2 jar包. 如果读者不确定要引用哪个jar包,则可以在Eclipse项目中Axis2分发包的lib目录中引用所有jar包.
2. 在此示例中,RPCServiceClient类的invokeBlocking方法用于调用WebService中的方法. invokeBlocking方法具有三个参数,其中第一个参数的类型是QName对象,它表示要调用的方法的名称. 第二个参数表示要调用的WebService方法的参数值,参数类型为Object []. 第三个参数表示参数类型为Class []的WebService方法的返回类型的Class对象. 当该方法没有参数时,invokeBlocking方法的第二个参数的值不能为null. 而是使用新的Object [] {}.
3. 如果被调用的WebService方法未返回值,则应使用RPCServiceClient类的invokeRobust方法. 此方法只有两个参数,其含义与invokeBlocking方法的前两个参数的含义相同.

4. 创建QName对象时,QName类的构造函数的第一个参数表示WSDL文件的名称空间名称,它是<wsdl: definitions>元素的targetNamespace属性的值. 以下是SimpleService类生成的WSDL文件. 代码段:
-> <? xmlversion =“ 1.0” encoding =“ UTF-8”?>
xmlns: ns =“ http://ws.apache.org/axis2” xmlns: wsaw =“ http://www.w3.org/2006/05/addressing/wsdl” xmlns: http =“ http://schemas.xmlsoap.org/wsdl/http/” xmlns: xs =“ http://www.w3.org/2001/XMLSchema” xmlns: mime =“ http://schemas.xmlsoap.org/wsdl/mime/” xmlns: soap =“ http://schemas.xmlsoap.org/wsdl/soap/” xmlns: soap12 =“ http://schemas.xmlsoap.org/wsdl/soap12/” targetNamespace =“ http://ws.apache.org/axis2”> wsdl: 类型> wsdl: 定义>


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shouji/article-154622-1.html
这种岛礁是不允许声称十二海里领海权的
军队其实是农民军
无论是民间还是当官的都滋长了一种戾气