1.下載axis,解壓後把webapps中的axis文件夾copy到web服務器中的webappa下,這樣就可以瀏覽了。
Axis支持三種web service的部署和開發,分別爲:
1、Dynamic Invocation Interface ( DII)
2、Stubs方式
3、Dynamic Proxy方式
2.http://localhost/axis/services 可以查看當前的服務。
3.第一個小程序:( DII)
服務器端:(保存爲Test.jws在上面提到的axis文件夾下)
public class Test{
//fields
private String name="gaga";
private int age=20;
private String message;
private List items=new ArrayList();
//method at here.
public String getName(){
return name;
}
public int getAge(){
return age;
}
public List getItems(){
return items;
}
public void setMessage(String message){
this.message=message;
System.out.println(message);
}
}
訪問連接http://localhost/axis/Test.jws?wsdl :頁面顯示Axis自動生成的wsdl
client端程序:
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
public class TestWebService {
public static void main(String args[]) {
System.out.println("Start invoking.");
try {
String endpoint = "http://localhost/axis/Test.jws";// 你寫的那個文件
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName("getAge");// 填寫你要調用的方法名稱
int ret = Integer.parseInt(("" + call.invoke(new Object[] {})));
System.out.println(ret);
call.setOperationName("setMessage"); //代參數的調用
// new Object[]{"test","test2"}傳遞多個參數
Object [] arguments=new Object [1];
arguments[0]="hello";
call.invoke(arguments);
} catch (Exception e) {
System.err.println(e.toString());
}
System.out.println("Finished the invoking.");
}
}
運行結果:
Start invoking....
20
Finished the invoking.
同時:服務器端輸出hello
實例2(Dynamic Proxy)
1.在axis/src下新建MyServiceInterface.java,MyService
import java.rmi.RemoteException;
public interface MyServiceInterface extends Remote {
public String processService(String arg) throws RemoteException;
}
public String processService(String arg){
return arg; }
}
然後將MyService copy到axis下保存爲MyService.jws
2.client端代碼如下:
String wsdlUrl = "http://localhost:8081/axis_example/jws/MyService.jws?wsdl";
String nameSpaceUri = "http://localhost:8081/axis_example/jws/MyService.jws";
String serviceName = "MyServiceService";
ServiceFactory serviceFactory = ServiceFactory.newInstance();
javax.xml.rpc.Service service = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUri, serviceName));
MyServiceInterface proxy = (MyServiceInterface)
service.getPort(new QName(nameSpaceUri, portName), MyServiceInterface.class);
System.out.println("This is " + proxy.processService("Dynamic Proxy test!"));
}
實例3(stubs):
1.在工程文件夾下建立Myservice:
public String processService(String arg){
return arg; }
}
2.新建deploy.wsdd(參考axis-bin-1_4.zip \axis-1_4\samples\deploy.wsdd)
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="MyService" provider="java:RPC">
<parameter name="className" value="MyService"/>
<parameter name="allowedMethods" value="processService"/>
</service>
</deployment>
3.啓動服務器,執行
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8081/axis_example/servlet/AxisServlet deploy.wsdd
執行後可以看到在web-inf下生成server-config.wsdd
4。重啓服務。
5。更改client.
// 指出service所在URL
String endpoint = "http://localhost:" + "8081" + "/axis_example/services/MyService";
// 創建一個服務(service)調用(call)
Service service = new Service();
Call call = (Call) service.createCall();// 通過service創建call對象
// 設置service所在URL
call.setTargetEndpointAddress(new java.net.URL(endpoint));
// 方法名(processService)與MyService.java方法名保持一致
call.setOperationName("processService");
// Object 數組封裝了參數,參數爲"This is Test!",調用processService(String arg)
String ret = (String) call.invoke(new Object[]{"This is Test!"});
System.out.println(ret);
}
注: 在這裏可以看出, DII 方式安全性不高(url MyService.jws爲axis自動生成),且無法進行一些複雜的配置, Dynamic Invocation Interface(DII) 和 Stubs 方式的區別主要有兩個地方:
① 兩種不同的 endpoint
DII :http://localhost:8081/axis_example/jws/MyService.jws
Stubs :http://localhost:8081/axis_example/services/MyService
② 兩種不同的編譯方式
DII :根據endpoint訪問web service時,axis自動編譯endpoint指定的*.jws文件,並放在生成的WEB-INF/jwsClasses目錄下。
Stubs :手工編譯java文件,手工編寫server-config.wsdd配置文件(這裏可以編寫deploy.wsdd,用axis提供的java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8081/axis_example/servlet/AxisServlet deploy.wsdd 命令生成server-config.wsdd文件中的其他通用部分)
而Dynamic Proxy方式僅僅在DII的基礎上採用了代理機制,實際上和DII區別不大。