java axis web service

编写 java调用web service的客户端比较简单,其中webservice为上一篇gsoap创建的server。

 1 package clientTest;
 2 import java.rmi.RemoteException;  
 3 
 4 import javax.xml.namespace.QName;  
 5 import javax.xml.rpc.ServiceException;  
 6 import javax.xml.rpc.ParameterMode;
 7 import javax.xml.rpc.encoding.XMLType;
 8 
 9 import org.apache.axis.client.Call;
10 import org.apache.axis.client.Service; 
11 public class ClientTest
12 {
13     public static void main(String[] args) {  
14         try {  
15             String endpoint = "http://localhost:8080";   
16             Service service = new Service();   
17             Call call = (Call)service.createCall();   
18             call.setTargetEndpointAddress(endpoint);      
19   
20             call.setOperationName(new QName(
21                     "urn:calculator", "add"));   
22             call.addParameter("a",XMLType.SOAP_DOUBLE,ParameterMode.IN);
23             call.addParameter("b", XMLType.SOAP_DOUBLE, ParameterMode.IN);
24             call.setReturnClass(Double.class);   
25             Double temp = (Double)call.invoke(new Object[]{2,2});   //传入的参数(a、b),返回的类型为Double
26             System.out.println("result: " + temp);  
27         } catch (ServiceException e) {  
28             // TODO Auto-generated catch block  
29             e.printStackTrace();  
30         } catch (RemoteException e) {  
31             // TODO Auto-generated catch block  
32             e.printStackTrace();  
33         }  
34     }  
35 }

所对应的wsdl文件如下:

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <definitions name="calculator"
  3  targetNamespace="urn:calculator"
  4  xmlns:tns="urn:calculator"
  5  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  6  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  7  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  9  xmlns:ns="urn:calculator"
 10  xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
 11  xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/"
 12  xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/"
 13  xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/"
 14  xmlns="http://schemas.xmlsoap.org/wsdl/">
 15 
 16 <types>
 17 
 18  <schema targetNamespace="urn:calculator"
 19   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 20   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
 21   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 22   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 23   xmlns:ns="urn:calculator"
 24   xmlns="http://www.w3.org/2001/XMLSchema"
 25   elementFormDefault="unqualified"
 26   attributeFormDefault="unqualified">
 27   <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
 28  </schema>
 29 
 30 </types>
 31 
 32 <message name="addRequest">
 33  <part name="a" type="xsd:double"/><!-- ns__add::a -->
 34  <part name="b" type="xsd:double"/><!-- ns__add::b -->
 35 </message>
 36 
 37 <message name="addResponse">
 38  <part name="result" type="xsd:double"/><!-- ns__add::result -->
 39 </message>
 40 
 41 <message name="subRequest">
 42  <part name="a" type="xsd:double"/><!-- ns__sub::a -->
 43  <part name="b" type="xsd:double"/><!-- ns__sub::b -->
 44 </message>
 45 
 46 <message name="subResponse">
 47  <part name="result" type="xsd:double"/><!-- ns__sub::result -->
 48 </message>
 49 
 50 <message name="sqrtRequest">
 51  <part name="a" type="xsd:double"/><!-- ns__sqrt::a -->
 52 </message>
 53 
 54 <message name="sqrtResponse">
 55  <part name="result" type="xsd:double"/><!-- ns__sqrt::result -->
 56 </message>
 57 
 58 <portType name="calculatorPortType">
 59  <operation name="add">
 60   <documentation>Service definition of function ns__add</documentation>
 61   <input message="tns:addRequest"/>
 62   <output message="tns:addResponse"/>
 63  </operation>
 64  <operation name="sub">
 65   <documentation>Service definition of function ns__sub</documentation>
 66   <input message="tns:subRequest"/>
 67   <output message="tns:subResponse"/>
 68  </operation>
 69  <operation name="sqrt">
 70   <documentation>Service definition of function ns__sqrt</documentation>
 71   <input message="tns:sqrtRequest"/>
 72   <output message="tns:sqrtResponse"/>
 73  </operation>
 74 </portType>
 75 
 76 <binding name="calculator" type="tns:calculatorPortType">
 77  <SOAP:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
 78  <operation name="add">
 79   <SOAP:operation style="rpc" soapAction=""/>
 80   <input>
 81      <SOAP:body use="encoded" namespace="urn:calculator" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
 82   </input>
 83   <output>
 84      <SOAP:body use="encoded" namespace="urn:calculator" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
 85   </output>
 86  </operation>
 87  <operation name="sub">
 88   <SOAP:operation style="rpc" soapAction=""/>
 89   <input>
 90      <SOAP:body use="encoded" namespace="urn:calculator" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
 91   </input>
 92   <output>
 93      <SOAP:body use="encoded" namespace="urn:calculator" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
 94   </output>
 95  </operation>
 96  <operation name="sqrt">
 97   <SOAP:operation style="rpc" soapAction=""/>
 98   <input>
 99      <SOAP:body use="encoded" namespace="urn:calculator" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
100   </input>
101   <output>
102      <SOAP:body use="encoded" namespace="urn:calculator" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
103   </output>
104  </operation>
105 </binding>
106 
107 <service name="calculator">
108  <documentation>gSOAP 2.8.14 generated service definition</documentation>
109  <port name="calculator" binding="tns:calculator">
110   <SOAP:address location="http://localhost:8080"/>
111  </port>
112 </service>
113 
114 </definitions>

程序结构如图:

添加的jar下载地址为java_webservice(注意文件类型,javarpc-api独立一个,其余的均在axis压缩包中)。

 

posted @ 2015-01-29 11:58  idealing  阅读(297)  评论(0编辑  收藏  举报