Java調用webservice介面方法

來源:互聯網
上載者:User

 

1. Java調用webservice介面方法

webservice的 發布一般都是使用WSDL(web service descriptive language)檔案的樣式來發布的,在WSDL檔案裡面,包含這個webservice暴露在外面可供使用的介面。

我們也可以在以下網站找到許多 webservice provider列表, 你可以使用下面的URL來測試你的webservice程式。

 

http://www.webservicex.net/ws/default.aspx

 

這上面列出了70多個包括很多方面的free webservice provider,utilities->global weather就可以擷取全球的天氣預報。

 

下面我們來看Java如何通過WSDL檔案來調用這些web service(以 COPS WebSSO為例)

 

1.1 [Apache提供]直接通過AXIS調用遠端web service

 

我認為這種調用方式適合比較那種返回比較簡單的資料的service, 比如,天氣預報,這些內容肯定可以通過一個很簡單的xml來返回。還有就是WebSSO,返回的就是一個字串。

這種調用方式的好處就是簡單(開發簡單,調用簡單,只要service提供方不改動對外的方法介面,用戶端都無需有代碼帶動),無需對web service有太深瞭解,只要按照套路去掉用就可以了。

 

直接調用模式如下:

 

<<LogonClientWithURL.java>>

 

package ws.client;

 

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

 

 

public class LogonClientWithURL {

  public static void main(String args[])
throws
Exception {

        try {

                  String urlname = "http://192.168.194.23:9080/Logon/services/Logon?wsdl" ;

                  urlname = "http://192.168.194.23:9080/Logon/services/Logon";

                   

                  Service s = new  Service();

                  Call call = (Call) s.createCall();

                  call.setTimeout(new Integer(5000));

                  call.setOperation( "getSecurityToken" );

                  call.setTargetEndpointAddress(urlname);

                     

                  Object[] fn01 = { "john" , "john" , null ,null };

                  String val = (String)call.invoke(fn01);

                  System.out .println( "getSecurityToken(correct):"  + val);

                   

                  Object[] fn02 = { "john" , "john2" , null ,null };

                  String va2 = (String)call.invoke(fn02);

                  System.out .println( "getSecurityToken(wrong):"  + va2);

           

        } catch (Exception e) {

              //java.io.InterruptedIOException: Read timed out

              System.out.println(e.getMessage());

        }

 

  }

}

 

1.2 [Apache提供]使用WSDL2Java把WSDL檔案轉成本地類,然後像本地類一樣使用。

這種方式應該可以調用所有的webService。

同時這個調用方式適合那種業務比較複雜的Service (特別是公司專屬應用程式, 不是外部所有人都能無限制訪問那種Service),比如,這個Service返回的xml內容比較複雜,同時多個用戶端系統都需要訪問這個Service,在這種情況,service提供方可能會考慮返回一個java類。畢竟,每個用戶端都要解析xml也夠麻煩的,還不如讓service提供方返回一個類。

 

調用模式如下:

1)使用WSDL2Java把WSDL檔案轉成本地類。 我這裡寫了bat檔案:

<<WSDL2JAVA.bat>>

set Axis_Lib=.\lib

set Java_Cmd=D:\Dev\JDK\jdk1.4.2_12\bin\java -Djava.ext.dirs=%Axis_Lib%

set Output_Path=.\src

set Package=com.ubs.ws

%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% http:\\192.168.194.23:9080\Logon\services\Logon\wsdl\Logon.wsdl

Pause

 

運行直接產生以下java類

Logon.java

LogonService.java

LogonServiceLocator.java

LogonSoapBindingStub.java

 

2) <<LogonClientWithStub.java>>

package test.cis.client;

 

import test.cis.*;

 

public class LogonClientWithStub {

 

      public static void main(String[] args) {

            try {

 

                  Logon locator = new LogonServiceLocator().getLogon();

                  String result = locator.getSecurityToken("john" , "john" ,null ,
null );

 

                  System.out.println("getSecurityToken(correct): " + result);

                 

                 

                  result = locator.getSecurityToken("john" , "john2" ,null ,
null );

 

                  System.out.println("getSecurityToken(wrong): " + result);

                 

            } catch (Exception e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

            }                      

           

      }

 

}

 

1.3 [Apache提供]直接SOAP調用遠端webservice

apache的soap項目,不過如今已經被axis取代了,後者重新進行了架構設計,功能更齊全。

前者已停止開發。所以建議用axis。也就是1.1 & 1.3

 

1.4 [XFire提供] 使用XFire調用WebService

有興趣的可以下載XFire的jar來學習一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.