Java調用webservice接口方法


                         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) {

                  e.printStackTrace();

            }                      

      }

}


免責聲明!

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



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