WebService是为程序服务的,只在浏览器中访问WebService是没有意义的。因此,在本节使用Java实现了一个控制台程序来调用上一节发布的WebService。
新建一个工程(java project或者Dynamic Web project),如图:
客户端代码如下:(将 axis2-1.6.2\lib下的所有jar放到工程的lib目录下)
1 package com.jyu.webservice;
2
3 import javax.xml.namespace.QName;
4 import org.apache.axis2.addressing.EndpointReference;
5 import org.apache.axis2.client.Options;
6 import org.apache.axis2.rpc.client.RPCServiceClient;
7
8 public class RPCClient
9 {
10 public static void main(String[] args) throws Exception
11 {
12 // 使用RPC方式调用WebService
13 RPCServiceClient serviceClient = new RPCServiceClient();
14 Options options = serviceClient.getOptions();
15 // 指定调用WebService的URL
16 EndpointReference targetEPR = new EndpointReference(
17 "http://localhost:8080/axis2/services/SimpleService");
18 options.setTo(targetEPR);
19 // 指定getGreeting方法的参数值
20 Object[] opAddEntryArgs = new Object[] {"Damon.Huang"};
21 // 指定getGreeting方法返回值的数据类型的Class对象
22 @SuppressWarnings("rawtypes")
23 Class[] classes = new Class[] {String.class};
24 // 指定要调用的getGreeting方法及WSDL文件的命名空间
25 QName opAddEntry = new QName("http://ws.apache.org/axis2", "getGreeting");
26 // 调用getGreeting方法并输出该方法的返回值
27 System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
28
29 // 下面是调用getPrice方法的代码,这些代码与调用getGreeting方法的代码类似
30 classes = new Class[] {int.class};
31 opAddEntry = new QName("http://ws.apache.org/axis2", "getPrice");
32 System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[]{}, classes)[0]);
33 }
34 }
运行后台输出如图:
在编写客户端代码时应注意如下几点:
1. 客户端代码需要引用很多Axis2的jar包,如果读者不太清楚要引用哪个jar包,可以在Eclipse的工程中引用Axis2发行包的lib目录中的所有jar包。
2. 在本例中使用了RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。
3. 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。
4. 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值,下面是SimpleService类生成的WSDL文件的代码片段
1 This XML file does not appear to have any style information associated with it. The document tree is shown below.
2 <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" 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">
3 <wsdl:types>
4 <xs:schema attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="http://ws.apache.org/axis2">
5 <xs:element name="getPrice">
6 <xs:complexType>
7 <xs:sequence/>
8 </xs:complexType>
9 </xs:element>
10 <xs:element name="getPriceResponse">
11 <xs:complexType>
12 <xs:sequence>
13 <xs:element minOccurs="0" name="return" type="xs:int"/>
14 </xs:sequence>
15 </xs:complexType>
16 </xs:element>
17 <xs:element name="getGreeting">
18 <xs:complexType>
19 <xs:sequence>
20 <xs:element minOccurs="0" name="args0" nillable="true" type="xs:string"/>
21 </xs:sequence>
22 </xs:complexType>
23 </xs:element>
24 <xs:element name="getGreetingResponse">
25 <xs:complexType>
26 <xs:sequence>
27 <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
28 </xs:sequence>
29 </xs:complexType>
30 </xs:element>
31 </xs:schema>
32 </wsdl:types>
33 <wsdl:message name="getPriceRequest">
34 <wsdl:part name="parameters" element="ns:getPrice"/>
35 </wsdl:message>
36 <wsdl:message name="getPriceResponse">
37 <wsdl:part name="parameters" element="ns:getPriceResponse"/>
38 </wsdl:message>
39 <wsdl:message name="getGreetingRequest">
40 <wsdl:part name="parameters" element="ns:getGreeting"/>
41 </wsdl:message>
42 <wsdl:message name="getGreetingResponse">
43 <wsdl:part name="parameters" element="ns:getGreetingResponse"/>
44 </wsdl:message>
45 <wsdl:portType name="SimpleServicePortType">
46 <wsdl:operation name="getPrice">
47 <wsdl:input message="ns:getPriceRequest" wsaw:Action="urn:getPrice"/>
48 <wsdl:output message="ns:getPriceResponse" wsaw:Action="urn:getPriceResponse"/>
49 </wsdl:operation>
50 <wsdl:operation name="getGreeting">
51 <wsdl:input message="ns:getGreetingRequest" wsaw:Action="urn:getGreeting"/>
52 <wsdl:output message="ns:getGreetingResponse" wsaw:Action="urn:getGreetingResponse"/>
53 </wsdl:operation>
54 </wsdl:portType>
55 <wsdl:binding name="SimpleServiceSoap11Binding" type="ns:SimpleServicePortType">
56 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
57 <wsdl:operation name="getPrice">
58 <soap:operation soapAction="urn:getPrice" style="document"/>
59 <wsdl:input>
60 <soap:body use="literal"/>
61 </wsdl:input>
62 <wsdl:output>
63 <soap:body use="literal"/>
64 </wsdl:output>
65 </wsdl:operation>
66 <wsdl:operation name="getGreeting">
67 <soap:operation soapAction="urn:getGreeting" style="document"/>
68 <wsdl:input>
69 <soap:body use="literal"/>
70 </wsdl:input>
71 <wsdl:output>
72 <soap:body use="literal"/>
73 </wsdl:output>
74 </wsdl:operation>
75 </wsdl:binding>
76 <wsdl:binding name="SimpleServiceSoap12Binding" type="ns:SimpleServicePortType">
77 <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
78 <wsdl:operation name="getPrice">
79 <soap12:operation soapAction="urn:getPrice" style="document"/>
80 <wsdl:input>
81 <soap12:body use="literal"/>
82 </wsdl:input>
83 <wsdl:output>
84 <soap12:body use="literal"/>
85 </wsdl:output>
86 </wsdl:operation>
87 <wsdl:operation name="getGreeting">
88 <soap12:operation soapAction="urn:getGreeting" style="document"/>
89 <wsdl:input>
90 <soap12:body use="literal"/>
91 </wsdl:input>
92 <wsdl:output>
93 <soap12:body use="literal"/>
94 </wsdl:output>
95 </wsdl:operation>
96 </wsdl:binding>
97 <wsdl:binding name="SimpleServiceHttpBinding" type="ns:SimpleServicePortType">
98 <http:binding verb="POST"/>
99 <wsdl:operation name="getPrice">
100 <http:operation location="getPrice"/>
101 <wsdl:input>
102 <mime:content type="application/xml" part="parameters"/>
103 </wsdl:input>
104 <wsdl:output>
105 <mime:content type="application/xml" part="parameters"/>
106 </wsdl:output>
107 </wsdl:operation>
108 <wsdl:operation name="getGreeting">
109 <http:operation location="getGreeting"/>
110 <wsdl:input>
111 <mime:content type="application/xml" part="parameters"/>
112 </wsdl:input>
113 <wsdl:output>
114 <mime:content type="application/xml" part="parameters"/>
115 </wsdl:output>
116 </wsdl:operation>
117 </wsdl:binding>
118 <wsdl:service name="SimpleService">
119 <wsdl:port name="SimpleServiceHttpSoap11Endpoint" binding="ns:SimpleServiceSoap11Binding">
120 <soap:address location="http://localhost:8080/axis2/services/SimpleService.SimpleServiceHttpSoap11Endpoint/"/>
121 </wsdl:port>
122 <wsdl:port name="SimpleServiceHttpSoap12Endpoint" binding="ns:SimpleServiceSoap12Binding">
123 <soap12:address location="http://localhost:8080/axis2/services/SimpleService.SimpleServiceHttpSoap12Endpoint/"/>
124 </wsdl:port>
125 <wsdl:port name="SimpleServiceHttpEndpoint" binding="ns:SimpleServiceHttpBinding">
126 <http:address location="http://localhost:8080/axis2/services/SimpleService.SimpleServiceHttpEndpoint/"/>
127 </wsdl:port>
128 </wsdl:service>
129 </wsdl:definitions>
View Code
转载于:https://www.cnblogs.com/damonhuang/archive/2013/05/27/3102676.html