1. 程式人生 > >eclipse+webservice開發例項

eclipse+webservice開發例項

今天利用eclipse釋出webService遇到問題,在網上找到了很多釋出的部落格,都沒有特別說明版本問題,儘管步驟都一樣,別人可以用自己不能,主要因為版本問題

1:首先版本對應Axis2-1.6. 對應tomcat也應該是1.6;版本對應Axis2-1.7對應tomcat也應該是1.7並且專案版本為3.0

參考部落格http://blog.csdn.net/xw13106209/article/details/7049614/             非常感謝

下面是具體的釋出流程
  

   1.1下載對應的Axis2版本

     網址http://axis.apache.org/axis2/java/core/download.cgi ,選擇Standard Binary Distribution的zip包,解壓縮得到的目錄名axis2-1.4.1,目錄內的檔案結構如下:
    

1.2.開發前配置:

在Eclipse的選單欄中,Window --> Preferences --> Web Service --> Axis2 Perferences,在Axis2 runtime location中選擇Axis2解壓縮包的位置,設定好後,點"OK"即行。(如圖

1.3開發服務

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

  1. package edu.sjtu.webservice;  
  2. /** 
  3.  * 計算器運算 
  4.  * @author rongxinhua
     
  5.  */
  6. publicclass CalculateService {  
  7.     //加法
  8.     publicfloat plus(float x, float y) {  
  9.         return x + y;  
  10.     }  
  11.     //減法
  12.     publicfloat minus(float x, float y) {  
  13.         return x - y;  
  14.     }  
  15.     //乘法
  16.     publicfloat multiply(float x, float y) {  
  17.         return x * y;  
  18.     }  
  19.     //除法
  20.     publicfloat 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話框。

1.4配置

在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伺服器了。

1.5等啟完後,點選"next -- > next",一切預設即行,最後,點選完成。最後,出現如下介面:(Web Service Explorer),我們在這裡便可測試我們的Web服務。


注:在瀏覽器中開啟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
  1. package edu.sjtu.webservice.test;  
  2. import javax.xml.namespace.QName;  
  3. import org.apache.axis2.AxisFault;  
  4. import org.apache.axis2.addressing.EndpointReference;  
  5. import org.apache.axis2.client.Options;  
  6. import org.apache.axis2.rpc.client.RPCServiceClient;  
  7. publicclass CalculateServiceTest {  
  8.     /** 
  9.      * @param args 
  10.      * @throws AxisFault 
  11.      */
  12.     publicstaticvoid main(String[] args) throws AxisFault {  
  13.         // TODO Auto-generated method stub
  14.         // 使用RPC方式呼叫WebService
  15.         RPCServiceClient serviceClient = new RPCServiceClient();  
  16.         Options options = serviceClient.getOptions();  
  17.         // 指定呼叫WebService的URL
  18.         EndpointReference targetEPR = new EndpointReference(  
  19.                 "http://localhost:8080/WebServiceTest1/services/CalculateService");  
  20.         options.setTo(targetEPR);  
  21.         // 指定要呼叫的計算機器中的方法及WSDL檔案的名稱空間:edu.sjtu.webservice。
  22.         QName opAddEntry = new QName("http://webservice.sjtu.edu","plus");//加法  
  23.         QName opAddEntryminus = new QName("http://webservice.sjtu.edu","minus");//減法  
  24.         QName opAddEntrymultiply = new QName("http://webservice.sjtu.edu","multiply");//乘法  
  25.         QName opAddEntrydivide = new QName("http://webservice.sjtu.edu","divide");//除法  
  26.         // 指定plus方法的引數值為兩個,分別是加數和被加數
  27.         Object[] opAddEntryArgs = new Object[] { 1,2 };  
  28.         // 指定plus方法返回值的資料型別的Class物件
  29.         Class[] classes = new Class[] { float.class };  
  30.         // 呼叫plus方法並輸出該方法的返回值
  31.         System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);  
  32.         System.out.println(serviceClient.invokeBlocking(opAddEntryminus,opAddEntryArgs, classes)[0]);  
  33.         System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,opAddEntryArgs, classes)[0]);  
  34.         System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,opAddEntryArgs, classes)[0]);  
  35.     }