系统功能: 开发一个计算器服务CalculateService,这个服务包含加(plus)、减(minus)、乘(multiply)
、除(divide)的操作。

开发前准备:
1、安装Eclipse-jee;
2、下载Axis2的最新版本Axis2 1.4.1 Release,网址
http://ws.apache.org/axis2/download/1_4_1/download.cgi ,选择Standard Binary Distribution
的.zip包即"axis2-1.4.1-bin.zip"这个文件,解压缩得到的目录名axis2-1.4.1,目录内的文件结构如
下:

开发前配置:在Eclipse的菜单栏中,Window --> Preferences --> Web Service --> Axis2
Perferences,在Axis2 runtime location中选择Axis2解压缩包的位置,设置好后,点"OK"即行。(如图



开发Web Service:
1、新建一个Java Project,命名为"WS_01";
2、新建一个class,命名为"CalculateService",完整代码如下:
  1. package rong.service;   
  2.   
  3. /** *//**  
  4.  * 计算器运算  
  5.  * @author rongxinhua  
  6.  *  
  7.  */  
  8. public class CalculateService {   
  9.        
  10.     /** *//**  
  11.      * 加法运算  
  12.      * @param x 被加数  
  13.      * @param y 加数  
  14.      * @return x与y的和  
  15.      */  
  16.     public float plus(float x, float y){   
  17.         return x + y ;   
  18.     }   
  19.        
  20.     /** *//**  
  21.      * 减法运算  
  22.      * @param x 被减数  
  23.      * @param y 减数  
  24.      * @return x与y之差  
  25.      */  
  26.     public float minus(float x, float y){   
  27.         return x - y ;   
  28.     }   
  29.        
  30.     /** *//**  
  31.      * 乘法运算  
  32.      * @param x 被乘数  
  33.      * @param y 乘数  
  34.      * @return x与y的乘积  
  35.      */  
  36.     public float multiply(float x, float y){   
  37.         return x * y ;    
  38.     }   
  39.        
  40.     /** *//**  
  41.      * 除法运算  
  42.      * @param x 被除数  
  43.      * @param y 除数  
  44.      * @return x与y的商  
  45.      */  
  46.     public float divide(float x, float y){   
  47.         return x / y ;   
  48.     }   
  49.   
  50. }  

3、在"WS_01"项目上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即行。
8、到了Server startup对话框,有个按键"start server"(如下图),点击它,则可启动Tomcat服务器
了。

9、等启完后,点击"next -- > next",一切默认即行,最后,点击完成。最后,出现如下界面:(Web
Service Explorer),我们在这里便可测试我们的Web服务。


10、测试比较简单,例如,我们选择一个"plus"的Operation,出现下图,在x的输入框中输入2,在y的
输入框中输入3,点击"go",便会在status栏中显示结果5.0。其他方法的测试也类似。
到这里用Axis2与Eclipse整合开发的Web Service的服务端和客户端就成功了。你也试试吧!