eclipse+axis2+webservice開發執行個體

來源:互聯網
上載者:User

標籤:blog   http   io   os   使用   ar   java   strong   檔案   

1.參考文獻:

1.利用Java編寫簡單的WebService執行個體  http://nopainnogain.iteye.com/blog/791525

2.Axis2與Eclipse整合開發Web Service  http://tech.ddvip.com/2009-05/1242968642120461.html

3.http://blog.csdn.net/lightao220/article/details/3489015

4.http://clq9761.iteye.com/blog/976029

5.使用Eclipse+Axis2+Tomcat構建Web Services應用(執行個體講解篇)

2.執行個體1(主要看到[2])2.1.系統功能: 開發一個計算機服務CalculateService,這個服務包含加(plus)、減(minus)、乘(multiply)、除(divide)的操作。

2.2.開發前準備:

 

  1. 安裝Eclipse-jee;
  2. 下載最新版本的Axis2,網址http://axis.apache.org/axis2/java/core/download.cgi ,選擇Standard Binary Distribution的zip包,解壓縮得到的目錄名axis2-1.4.1,目錄內的檔案結構如下:

 

2.3.開發前配置:在Eclipse的功能表列中,Window --> Preferences --> Web Service --> Axis2 Perferences,在Axis2 runtime location中選擇Axis2解壓縮包的位置,設定好後,點"OK"即行。()

 

2.4.開發Web Service:

 

(1)建立一個Java Project,命名為"WebServiceTest1"
(2)建立一個class,命名為"CalculateService",完整代碼如下:

[java] view plaincopy 
  1. package edu.sjtu.webservice;  
  2. /** 
  3.  * 計算機運算 
  4.  * @author rongxinhua 
  5.  */  
  6. public class CalculateService {  
  7.     //加法  
  8.     public float plus(float x, float y) {  
  9.         return x + y;  
  10.     }  
  11.     //減法  
  12.     public float minus(float x, float y) {  
  13.         return x - y;  
  14.     }  
  15.     //乘法  
  16.     public float multiply(float x, float y) {  
  17.         return x * y;  
  18.     }  
  19.     //除法  
  20.     public float divide(float x, float y) {  
  21.         if(y!=0)  
  22.         {  
  23.             return x / y;  
  24.         }  
  25.         else  
  26.             return -1;  
  27.     }  
  28. }  

(3)在"WebServiceTest1"項目上new --> other,找到"Web Services"下面的"Web Service";

(4)下一步(next),在出現的Web Services對象框,在Service implementation中點擊"Browse",進入Browse Classes對象框,尋找到我們剛才寫的寫的CalculateService類。(如)。點擊"ok",則回到Web Service話框。

(5)在Web Service對話方塊中,將Web Service type中的滑塊,調到"start service“的位置,將Client type中的滑塊調到"Test client"的位置。

(6)在Web Service type滑塊圖的右邊有個"Configuration",點擊它下面的選項,進入Service Deployment Configuration對象框,在這裡選擇相應的Server(我這裡用Tomcat6.0)和Web Service runtime(選擇Apache Axis2),如:

(7)點OK後,則返回到Web Service對話方塊,同理,Client type中的滑塊右邊也有"Configuration",也要進行相應的置,步驟同上。完成後,Next --> next即行。進入到Axis2 Web Service Java Bean Configuration,我們選擇Generate a default services.xml,如所示:

(8)到了Server startup對話方塊,有個按鍵"start server"(如),點擊它,則可啟動Tomcat伺服器了。

(9)等啟完後,點擊"next -- > next",一切預設即行,最後,點擊完成。最後,出現如下介面:(Web Service Explorer),我們在這裡便可測試我們的Web服務。(使用瀏覽器開啟的話使用如下地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=3)。如所示:

註:在瀏覽器中開啟Web Service Explorer(有時候在eclipse中關閉了webservice explorer,可以用這種方法開啟)

首先登入地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp。然後在網頁右上方選擇Web Service Exoplorer標籤。然後輸入WSDL地址:http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl 。這個wsdl地址就是我們剛才發布服務的那個wsdl。點擊go,如所示:

然後就可以看到如下介面了:

(10)測試比較簡單,例如,我們選擇一個"plus"的Operation(必須是CalculateServiceSoap11Binding),出現,在x的輸入框中輸入1,在y的輸入框中輸入2,點擊"go",便會在status欄中顯示結果3.0。其他方法的測試也類似。結果如所示。

2.5.CalculateService用戶端調用程式前面我們已經定義好了加減乘除的方法並將這些方法發布為服務,那麼現在要做的就是調用這些服務即可。用戶端調用程式如下代碼所示:CalculateServiceTest.java [java] view plaincopy 
  1. package edu.sjtu.webservice.test;  
  2.   
  3. import javax.xml.namespace.QName;  
  4. import org.apache.axis2.AxisFault;  
  5. import org.apache.axis2.addressing.EndpointReference;  
  6. import org.apache.axis2.client.Options;  
  7. import org.apache.axis2.rpc.client.RPCServiceClient;  
  8.   
  9. public class CalculateServiceTest {  
  10.   
  11.     /** 
  12.      * @param args 
  13.      * @throws AxisFault 
  14.      */  
  15.     public static void main(String[] args) throws AxisFault {  
  16.         // TODO Auto-generated method stub  
  17.   
  18.         // 使用RPC方式調用WebService  
  19.         RPCServiceClient serviceClient = new RPCServiceClient();  
  20.         Options options = serviceClient.getOptions();  
  21.         // 指定調用WebService的URL  
  22.         EndpointReference targetEPR = new EndpointReference(  
  23.                 "http://localhost:8080/WebServiceTest1/services/CalculateService");  
  24.         options.setTo(targetEPR);  
  25.   
  26.         // 指定要調用的電腦器中的方法及WSDL檔案的命名空間:edu.sjtu.webservice。  
  27.         QName opAddEntry = new QName("http://webservice.sjtu.edu","plus");//加法  
  28.         QName opAddEntryminus = new QName("http://webservice.sjtu.edu","minus");//減法  
  29.         QName opAddEntrymultiply = new QName("http://webservice.sjtu.edu","multiply");//乘法  
  30.         QName opAddEntrydivide = new QName("http://webservice.sjtu.edu","divide");//除法  
  31.         // 指定plus方法的參數值為兩個,分別是加數和被加數  
  32.         Object[] opAddEntryArgs = new Object[] { 1,2 };  
  33.         // 指定plus方法傳回值的資料類型的Class對象  
  34.         Class[] classes = new Class[] { float.class };  
  35.         // 調用plus方法並輸出該方法的傳回值  
  36.         System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);  
  37.         System.out.println(serviceClient.invokeBlocking(opAddEntryminus,opAddEntryArgs, classes)[0]);  
  38.         System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,opAddEntryArgs, classes)[0]);  
  39.         System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,opAddEntryArgs, classes)[0]);  
  40.   
  41.     }  
  42. }  
運行結果: [java] view plaincopy 
  1. 3.0  
  2. -1.0  
  3. 2.0  
  4. 0.5  
3.執行個體2.HelloService

(1)首先定義服務方法,代碼如下所示:

 

[java] view plaincopy 
  1. package edu.sjtu.webservice;  
  2.   
  3. public class HelloService {  
  4.     public String sayHelloNew() {  
  5.         return "hello";  
  6.     }  
  7.   
  8.     public String sayHelloToPersonNew(String name) {  
  9.         if (name == null) {  
  10.             name = "nobody";  
  11.         }  
  12.         return "hello," + name;  
  13.     }  
  14.   
  15.     public void updateData(String data) {  
  16.         System.out.println(data + " 已更新。");  
  17.     }  
  18. }  

(2)參考執行個體1將這個方法發布為服務。

 

(3)編寫用戶端代碼調用WebService(主要參考[5])

本文例子與其他例子最大的不同就在這裡,其他例子一般需要根據剛才的服務wsdl產生用戶端stub,然後通過stub來調用服務,這種方式顯得比較單一,用戶端必須需要stub存根才能夠訪問服務,很不方面。本例子的用戶端不採用stub方式,而是一種實現通用的調用方式,不需要任何用戶端存根即可訪問服務。只需要指定對於的web servce地址、操作名、參數和函數傳回型別即可。代碼如下所示:

HelloServiceTest2.java [java] view plaincopy 
  1. package edu.sjtu.webservice.test;  
  2.   
  3. import javax.xml.namespace.QName;  
  4.   
  5. import org.apache.axis2.AxisFault;  
  6. import org.apache.axis2.addressing.EndpointReference;  
  7. import org.apache.axis2.client.Options;  
  8. import org.apache.axis2.rpc.client.RPCServiceClient;  
  9.   
  10. public class HelloServiceTest2 {  
  11.     private RPCServiceClient serviceClient;  
  12.     private Options options;  
  13.     private EndpointReference targetEPR;  
  14.   
  15.     public HelloServiceTest2(String endpoint) throws AxisFault {  
  16.         serviceClient = new RPCServiceClient();  
  17.         options = serviceClient.getOptions();  
  18.         targetEPR = new EndpointReference(endpoint);  
  19.         options.setTo(targetEPR);  
  20.     }  
  21.   
  22.     public Object[] invokeOp(String targetNamespace, String opName,  
  23.             Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,  
  24.             ClassNotFoundException {  
  25.         // 設定操作的名稱  
  26.         QName opQName = new QName(targetNamespace, opName);  
  27.         // 設定傳回值  
  28.         // Class<?>[] opReturn = new Class[] { opReturnType };  
  29.         // 操作需要傳入的參數已經在參數中給定,這裡直接傳入方法中調用  
  30.         return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);  
  31.     }  
  32.   
  33.     /** 
  34.      * @param args 
  35.      * @throws AxisFault 
  36.      * @throws ClassNotFoundException 
  37.      */  
  38.     public static void main(String[] args) throws AxisFault,  
  39.             ClassNotFoundException {  
  40.         // TODO Auto-generated method stub  
  41.         final String endPointReference = "http://localhost:8080/WebServiceTest1/services/HelloService";  
  42.         final String targetNamespace = "http://webservice.sjtu.edu";  
  43.         HelloServiceTest2 client = new HelloServiceTest2(endPointReference);  
  44.   
  45.         String opName = "sayHelloToPersonNew";  
  46.         Object[] opArgs = new Object[] { "My Friends" };  
  47.         Class<?>[] opReturnType = new Class[] { String[].class };  
  48.   
  49.         Object[] response = client.invokeOp(targetNamespace, opName, opArgs,  
  50.                 opReturnType);  
  51.         System.out.println(((String[]) response[0])[0]);  
  52.     }  
  53.   
  54. }  

運行該程式,點擊Run As->Java application,可以看到控制台連接埠的輸出是:Hello, My Friends,表明用戶端調用成功。該例子最大的不同和優勢表現在用戶端的調用方式,或者說是發起服務調用的方式,雖然比起用戶端stub存根的方式,代碼稍多,但是這種方式統一,不需要生產stub存根代碼,解決了用戶端有很多類的問題。如果讀者對這些代碼進一步封裝,我想調用方式很簡單,只需要傳遞相關參數,這更好地說明了服務調用的優勢。而且這種方式更加簡單明了,一看便知具體含義。而不需要弄得stub類的一些機制。

(4)改寫用戶端調用服務的代碼

(3)中提到的用戶端應用代碼寫的略微有些繁雜,下面將上面的用戶端調用service程式進行改寫,簡潔了許多。代碼如下:

HelloServiceTest.java

 

[java] view plaincopy 
    1. import javax.xml.namespace.QName;  
    2. import org.apache.axis2.AxisFault;  
    3. import org.apache.axis2.addressing.EndpointReference;  
    4. import org.apache.axis2.client.Options;  
    5. import org.apache.axis2.rpc.client.RPCServiceClient;  
    6.   
    7. public class HelloServiceTest {  
    8.     public static void main(String args[]) throws AxisFault {  
    9.         // 使用RPC方式調用WebService  
    10.         RPCServiceClient serviceClient = new RPCServiceClient();  
    11.         Options options = serviceClient.getOptions();  
    12.         // 指定調用WebService的URL  
    13.         EndpointReference targetEPR = new EndpointReference("http://localhost:8080/WebServiceTest1/services/HelloService");  
    14.         options.setTo(targetEPR);  
    15.           
    16.         // 指定要調用的sayHelloToPerson方法及WSDL檔案的命名空間  
    17.         QName opAddEntry = new QName("http://webservice.sjtu.edu","sayHelloToPersonNew");  
    18.         // 指定sayHelloToPerson方法的參數值  
    19.         Object[] opAddEntryArgs = new Object[] { "xuwei" };  
    20.         // 指定sayHelloToPerson方法傳回值的資料類型的Class對象  
    21.         Class[] classes = new Class[] { String.class };  
    22.         // 調用sayHelloToPerson方法並輸出該方法的傳回值  
    23.         System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);  
    24.     }  
    25. }  

eclipse+axis2+webservice開發執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.