java 調用wsdl的方式


下面我們來看Java如何通過WSDL文件來調用這些web service:

注意,以下的代碼並沒有經過真正的測試,只是說明這些情況,不同版本的Axis相差很大,大家最好以apache網站上的例子為准,這里僅僅用於說明其基本用法。

1,直接AXIS調用遠程的web service

我覺得這種方法比較適合那些高手,他們能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是專門搞這行的,即使一段時間看懂,后來也就忘記了。直接調用模式如下:

Java代碼 復制代碼  收藏代碼
  1. import java.util.Date;   
  2. import java.text.DateFormat;   
  3. import org.apache.axis.client.Call;   
  4. import org.apache.axis.client.Service;   
  5. import javax.xml.namespace.QName;   
  6. import java.lang.Integer;   
  7. import javax.xml.rpc.ParameterMode;   
  8.     
  9. public class caClient {   
  10.                
  11.        public static void main(String[] args) {   
  12.     
  13.               try {   
  14.                      String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";   
  15.                      //直接引用遠程的wsdl文件   
  16.                     //以下都是套路    
  17.                      Service service = new Service();   
  18.                      Call call = (Call) service.createCall();   
  19.                      call.setTargetEndpointAddress(endpoint);   
  20.                      call.setOperationName("addUser");//WSDL里面描述的接口名稱   
  21.                      call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,   
  22.                                    javax.xml.rpc.ParameterMode.IN);//接口的參數   
  23.                      call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//設置返回類型     
  24.                      String temp = "測試人員";   
  25.                      String result = (String)call.invoke(new Object[]{temp});   
  26.                      //給方法傳遞參數,並且調用方法   
  27.                      System.out.println("result is "+result);   
  28.               }   
  29.               catch (Exception e) {   
  30.                      System.err.println(e.toString());   
  31.               }   
  32.        }   
  33. }  
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
 
public class caClient {
            
       public static void main(String[] args) {
 
              try {
                     String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
                     //直接引用遠程的wsdl文件
                    //以下都是套路 
                     Service service = new Service();
                     Call call = (Call) service.createCall();
                     call.setTargetEndpointAddress(endpoint);
                     call.setOperationName("addUser");//WSDL里面描述的接口名稱
                     call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
                                   javax.xml.rpc.ParameterMode.IN);//接口的參數
                     call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//設置返回類型  
                     String temp = "測試人員";
                     String result = (String)call.invoke(new Object[]{temp});
                     //給方法傳遞參數,並且調用方法
                     System.out.println("result is "+result);
              }
              catch (Exception e) {
                     System.err.println(e.toString());
              }
       }
}


2,直接SOAP調用遠程的webservice
這種模式我從來沒有見過,也沒有試過,但是網絡上有人貼出來,我也轉過來

Java代碼 復制代碼  收藏代碼
  1. import org.apache.soap.util.xml.*;   
  2. import org.apache.soap.*;   
  3. import org.apache.soap.rpc.*;   
  4.     
  5. import java.io.*;   
  6. import java.net.*;   
  7. import java.util.Vector;   
  8.     
  9. public class caService{   
  10.        public static String getService(String user) {   
  11.        URL url = null;   
  12.        try {   
  13.            url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");   
  14.        } catch (MalformedURLException mue) {   
  15.           return mue.getMessage();   
  16.          }   
  17.              // This is the main SOAP object   
  18.        Call soapCall = new Call();   
  19.        // Use SOAP encoding   
  20.        soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);   
  21.        // This is the remote object we're asking for the price   
  22.        soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");   
  23.        // This is the name of the method on the above object   
  24.        soapCall.setMethodName("getUser");   
  25.        // We need to send the ISBN number as an input parameter to the method   
  26.        Vector soapParams = new Vector();   
  27.     
  28.        // name, type, value, encoding style   
  29.        Parameter isbnParam = new Parameter("userName", String.class, user, null);   
  30.        soapParams.addElement(isbnParam);   
  31.        soapCall.setParams(soapParams);   
  32.        try {   
  33.           // Invoke the remote method on the object   
  34.           Response soapResponse = soapCall.invoke(url,"");   
  35.           // Check to see if there is an error, return "N/A"   
  36.           if (soapResponse.generatedFault()) {   
  37.               Fault fault = soapResponse.getFault();   
  38.              String f = fault.getFaultString();   
  39.              return f;   
  40.           } else {   
  41.              // read result   
  42.              Parameter soapResult = soapResponse.getReturnValue ();   
  43.              // get a string from the result   
  44.              return soapResult.getValue().toString();   
  45.           }   
  46.        } catch (SOAPException se) {   
  47.           return se.getMessage();   
  48.        }   
  49.     }   
  50. }  
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
 
import java.io.*;
import java.net.*;
import java.util.Vector;
 
public class caService{
       public static String getService(String user) {
       URL url = null;
       try {
           url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
       } catch (MalformedURLException mue) {
          return mue.getMessage();
         }
             // This is the main SOAP object
       Call soapCall = new Call();
       // Use SOAP encoding
       soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
       // This is the remote object we're asking for the price
       soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
       // This is the name of the method on the above object
       soapCall.setMethodName("getUser");
       // We need to send the ISBN number as an input parameter to the method
       Vector soapParams = new Vector();
 
       // name, type, value, encoding style
       Parameter isbnParam = new Parameter("userName", String.class, user, null);
       soapParams.addElement(isbnParam);
       soapCall.setParams(soapParams);
       try {
          // Invoke the remote method on the object
          Response soapResponse = soapCall.invoke(url,"");
          // Check to see if there is an error, return "N/A"
          if (soapResponse.generatedFault()) {
              Fault fault = soapResponse.getFault();
             String f = fault.getFaultString();
             return f;
          } else {
             // read result
             Parameter soapResult = soapResponse.getReturnValue ();
             // get a string from the result
             return soapResult.getValue().toString();
          }
       } catch (SOAPException se) {
          return se.getMessage();
       }
    }
}


3,使用wsdl2java把WSDL文件轉成本地類,然后像本地類一樣使用,即可。
這是像我這種懶人最喜歡的方式,仍然以前面的global weather report為例。
首先   java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL
原本的網址是http://www.webservicex.net/globalweather.asmx?WSDL,中間個各問號,但是Linux下面它不能解析,所以去掉問號,改為點號。
那么就會出現4個文件:
GlobalWeather.java  GlobalWeatherLocator.java  GlobalWeatherSoap.java  GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我們最為關心的接口文件,如果你對RMI等SOAP實現的具體細節不感興趣,那么你只需要看接口文件即可,在使用的時候,引入這個接口即可,就好像使用本地類一樣。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM