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. 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话框。

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服务。

等启完后,点击"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
  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.     }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值