Axis是Apache的一個開源web service引擎。它目前最為成熟的開源web service引擎之一。下面我主要介紹一下如何使用該Axis開發您的web service 服務。
1.安裝
以tomcat4.1為伺服器,下面說明如何安裝axis:
1.解壓下載後的包,將包中axis目錄複寫到tomcat目錄下的webapps目錄下;
2.將axis/WEB-INF/lib目錄下類檔案複製到tomcat目錄下的common/lib目錄下;
3.重新啟動tomcat;
4.訪問http://localhost:8080/axis/happyaxis.jsp,如果能訪問,表示安裝成功;
2.開發webservice服務
a.編寫普通類檔案,如下所示://檔案名稱:Test.java
import java.util.*;
public class Test{
//fields
private String name="gaga";
private int age=20;
private List items=new ArrayList();
//method at here.
public String getName(){
return name;
}
public int getAge(){
return age;
}
public List getItems(){
return items;
}
}
將本檔案(Test.java)複製到Axis目錄下,並將其更名為Test.jws;
b.訪問http://localhost:8080/axis/Test.jws,Axis就會編譯該檔案,並將其部署到系統中;
下面你就可以開發web service的用戶端程式了;
3.開發用戶端程式
下面是用戶端程式:import org.apache.axis.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:8080/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);
} catch (Exception e) {
System.err.println(e.toString());
}
System.out.println("Finished the invoking.");
}
}
關於業務調用封裝到服務端程式裡,然後部署就可以了,就這麼簡單。
4.資源
http://ws.apache.org/axis/