目前看到的有三種比較簡單的方法:
1使用靜態的stub
通過wsdl2java工具,處理相應ws的wsdl文件,我們可以得到遠程ws的stub?直接調用這
個stub即可
AXIS提供的wsdl2java工具,如下:
java?org.apache.axis.wsdl.WSDL2Java?(WSDL-file-URL)
我們直接調用stub即可
eclipse也有相應的插件可以直接import?wsdl來產生stub,如果你安裝了EMF?all?in?one版本的eclipse?3.1,就可以在視圖里把Web?Service的相關視圖打開,接著就可以通過Web?Service的Wizard來創建出相關的Web?Service?Client或者Server
2?Dynamic?Proxy
根據遠程wsdl,利用javax.xml.rpc.Service的getPort函數,可以得到遠程ws的一個?D
ynamic?Proxy
編寫代理接口
public?interface?HelloClientInterface
extends?Java.rmi.Remote
{
????public?String?getName(String?name)
????????throws?Java.rmi.RemoteException;
}
?
客戶端程序TestHelloClient.Java?
import?Javax.xml.rpc.Service;
import?Javax.xml.rpc.ServiceFactory;
import?Java.net.URL;
import?Javax.xml.namespace.QName;
public?class?TestHelloClient?
{
????public?static?void?main(String[]?args)
????????{
????????try
????????{
???String?wsdlUrl?=?"http://localhost:8080/axis/HelloClient.jws?wsdl";
???String?nameSpaceUri?=?"http://localhost:8080/axis/HelloClient.jws";
String?serviceName?=?"HelloClientService";
String?portName?=?"HelloClient";
ServiceFactory?serviceFactory?=?ServiceFactory.newInstance();
Service?afService?=?serviceFactory.createService(new?URL(wsdlUrl),
???????????new?QName(nameSpaceUri,?serviceName));
?HelloClientInterface?proxy?=?(HelloClientInterface)afService.getPort(new?QName(
??????????????nameSpaceUri,?portName),?HelloClientInterface.class);
?System.out.println("return?value?is?"+proxy.getName("john")?)?;
????????}catch(Exception?ex)
????????{
????????????ex.printStackTrace()?;
????????}
????}
}
?
3?DII
Dynamic?Invocation?Interface?這個最好理解,比如你動態獲得了一個類,只知道類的
名字,你要調用他的一個方法,只好使用reflection得到你要調用的類,相應的參數信
息,然后調用
使用DII調用WS的時候,你知道的只是一個WSDL的地址,通過解析wsdl,你可以得到相應
的ws?endpoint的信息,然后通過javax.xml.rpc.Call的setOperationName,?addParam
eter等函數來指定要調用的函數,指定參數,然后調用
提供DII調用的原因是,我們有可能使用程序自動的去動態調用網絡上的WS,而這個WS的
一切信息都是來自其WSDL
,只有通過DII,我們才有可能動態的去調用這個ws
例子:
服務器端程序
public?class?HelloClient
{
????public?String?getName(String?name)
????{
????????return?"hello?"+name;
????}
}
把源碼拷貝到AXIS_HOME下,并改名為?HelloClient.jws
客戶端程序
import?org.apache.Axis.client.Call;
import?org.apache.Axis.client.Service;
import?Javax.xml.namespace.QName;
import?Javax.xml.rpc.ServiceException;
import?Java.net.MalformedURLException;
import?Java.rmi.RemoteException;
public?class?SayHelloClient2
{
????public?static?void?main(String[]?args)
????????{
????????try
????????????????{
??String?endpoint?=??"http://localhost:8080/axis/HelloClient.jws";
????Service?service?=?new?Service();
????????????Call?call?=?null;
????????????call?=?(Call)?service.createCall();
????????????call.setOperationName
???(new?QName("http://localhost:8080/axis/HelloClient.jws",?"getName"));
????????????call.setTargetEndpointAddress(new?Java.net.URL(endpoint));
?String?ret?=?
?(String)?call.invoke(new?Object[]?
?????{"zhangsan"});
??System.out.println
??("return?value?is?"?+?ret);
????????}?
????????????????catch?(Exception?ex)
????????????????{
???????ex.printStackTrace();
????????}
????}
}
posted on 2006-08-11 09:21
matthew 閱讀(1071)
評論(0) 編輯 收藏 所屬分類:
Web Services and SOA