初學axis

使用Axis2以普通的Java類建立Web Services

 

  Apache Axis2是一個Web Services系統服務和客戶端的實現。

  1、下載和部署Axis2。

    1)下載地址:http://axis.apache.org/axis2/java/core/download.cgi

 

    下載Binary Distribution和WAR Distribution這兩個壓縮包。

    2)將下載的axis2-1.6.2-war.zip解壓,解壓後將axis2.war複製到tomcat的webapps目錄下

    3)啓動tomcat,瀏覽器訪問:http://localhost:8080/axis2/ 出現以下界面,部署成功。

  

  2、以普通的Java類建立Web Services

    1)需要注意的:這個普通的Java類是沒有package的

    2)在\apache-tomcat-6.0.35\webapps\axis2\WEB-INF\目錄下建立pojo目錄。

     3)Java類。

    /** * 第一個Web Services  * @author Luxh */public class FirstService {

    public String sayHello(String name) {

    String result = "Hello,"+name+",this is first Axis2 Application!";

    return result;

    }

    public String getInfo() {

    return "This is first Axis2 Application!";

    }

    }

    將這個類編譯後的FirstService.class文件複製到\apache-tomcat-6.0.35\webapps\axis2\WEB-INF\pojo目錄下,無需重啓tomcat,Axis2支持熱部署。

    4)訪問:http://localhost:8080/axis2/services/listServices

    可以看到普通的Java類已經發布爲web Services

 

   5)訪問web Services

    http://localhost:8080/axis2/services/FirstService/getInfo

    其中 http://localhost:8080/axis2/services/FirstService/  是Web Services 的地址,getInfo是方法名稱

  其中 http://localhost:8080/axis2/services/FirstService/  是Web Services 的地址,sayHello是方法名稱,?name=LiHuai是參數名稱和參數值

  傳參數的時候要注意:一定要跟提供服務的方法中的參數名一致public String sayHello(String name)

   3、Java客戶端使用RPC方式調用Web Services

    1)新建一個Java Project,將axis2-1.6.2-bin.zip解壓,解壓後將\axis2-1.6.2\lib\目錄下的所有jar引入到項目的classpath路徑中

    2)客戶端代碼

    package cn.luxh.ws.client;import javax.xml.namespace.QName;import org.apache.axis2.AxisFault;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.rpc.client.RPCServiceClient;import org.junit.Test;public class FirstClient {

    /** * 用RPC方法調用Web Services * @throws AxisFault

    */@Testpublic void testGetServices() throws AxisFault {    //創建一個訪問服務的客戶端

    RPCServiceClient client = new RPCServiceClient();    //獲取操作客戶端選項的持有者

    Options options = client.getOptions();    //設置要調用的Web Services 的地址

    String ServiceEPR = "http://localhost:8080/axis2/services/FirstService";

    EndpointReference epr = new EndpointReference(ServiceEPR);

    options.setTo(epr);

    //因爲提供Web Services普通的Java類沒有package,

    //所以默認是http://ws.apache.org/axis2

    //getInfo是要調用的方法

    QName qName = new QName("http://ws.apache.org/axis2", "getInfo");

    //qName    --調用的方法

    //new Object[]{}--傳遞的參數值

    //new Class[]{}  --返回值類型

    Object[] result = client.invokeBlocking(qName, new Object[]{}, new Class[]{String.class});

    System.out.println("返回的結果是:"+result[0]);//調用sayHello方法

    qName = new QName("http://ws.apache.org/axis2", "sayHello");

    result = client.invokeBlocking(qName, new Object[]{"LiHuai"}, new Class[]{String.class});

    System.out.println("返回的結果是:"+result[0]);

    }}

 4、使用生成stub的方式調用Web Services

    可以使用wsdl2java命令生成調用Web Services的客戶端代碼

    wsdl2java命令格式:wsdl2java -uri http://localhost:8080/axis2/services/FirstService?wsdl -p cn.luxh.ws.client -s -o stub

    -uri 指定訪問的Web Services wsdl文件路徑

    -p 指定了生成的Java類的包名

    -o 指定了生成文件保存的根目錄

    1)在命令行進入到axis2-1.6.2-bin.zip解壓後的\axis2-1.6.2\bin\目錄,因爲wsdl2java命令在這個目錄中

    2)輸入 wsdl2java -uri http://localhost:8080/axis2/services/FirstService?wsdl -p client -s -o stub 然後回車

    3)執行完後,在\axis2-1.6.2\bin\目錄下有個stub文件夾,在stub文件夾下的src\cn\luxh\ws\client包下有個FirstServiceStub.java和FirstServiceUnsupportedEncodingExceptionException.java類,我們把這兩個類複製項目中相應的包下,就可以直接調用Web Services方法了。

    4)stub客戶端代碼

    package cn.luxh.ws.client;import java.rmi.RemoteException;import javax.xml.namespace.QName;import org.junit.Test;public class FirstClient {@Testpublic void testGetServiceByStub()
 throws RemoteException, FirstServiceUnsupportedEncodingExceptionException {FirstServiceStub firstServceStub = new FirstServiceStub();

    //wsdl2java命令將Web Services 的方法封裝成了靜態類

    //所以需要通過下面的方法獲取返回結果FirstServiceStub.GetInfo getInfo =  new FirstServiceStub.GetInfo();

    String ruselt = firstServceStub.getInfo(getInfo)。get_return();

    System.out.println("getInfo方法返回值:"+ruselt);

    FirstServiceStub.SayHello sayHello = new FirstServiceStub.SayHello();

    sayHello.setName("LiHuai");ruselt = firstServceStub.sayHello(sayHello)。get_return();

    System.out.println("sayHello方法返回值:"+ruselt);

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章