`

eclipse+Axis2+webservice开发实例

 
阅读更多

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+tomcat+axis下创建WebService

    在本文中,我们将深入探讨如何在 Eclipse 集成开发环境中,利用 Tomcat 服务器和 Axis 插件创建并发布一个 WebService。这是一个详细步骤的指南,涵盖了从环境准备到测试 WebService 的全过程。 首先,我们需要确保...

    eclipse+AXIS开发webservice.doc

    本文档主要介绍了如何使用Eclipse和AXIS框架开发和发布Web服务,以及进行客户端测试。以下是详细步骤和关键知识点: 1. **Eclipse插件安装**: - Eclipse提供了Web服务开发的插件,可以从官方网站下载WTP(Web ...

    AXIS2远程调用WebService示例(Eclipse+AXIS2)工具和所用包.rar

    本文将详细介绍如何使用Eclipse集成开发环境和AXIS2框架创建并调用WebService。首先,我们需要准备以下基础工具: 1. Eclipse IDE:这是一个强大的Java开发平台,支持多种开发任务,包括Web服务的开发和调试。 2. ...

    Eclipse开发Axis_WebService实例总结_20100430

    Eclipse 开发 Axis_WebService 实例总结

    axis2发布webservice和调用axis2服务接口

    这可以通过 Axis2的命令行工具或集成开发环境(IDE)如Eclipse中的插件完成。打包过程中,需要包含服务类、服务描述文件(如`services.xml`)和其他必要的依赖。 3. **部署服务**:将生成的`.aar`包放入Axis2服务器...

    axis2开发webservice

    标题 "Axis2 开发 WebService" 指的是使用 Apache Axis2 框架在 Eclipse 集成开发环境中创建和部署 WebService 的过程。Apache Axis2 是一个强大的 WebService 引擎,它提供了高性能、灵活且可扩展的架构,支持多种...

    用axis2搭建webservice

    这样,Axis2将在处理请求时使用Spring容器来实例化服务类。 6. **测试Web服务**:通过Axis2提供的内置Web服务器或自定义的HTTP服务器,可以测试Web服务是否正确运行。可以使用WSDL(Web Services Description ...

    eclipse&webservice开发实例.doc

    利用Java编写简单的WebService实例 Axis2与Eclipse整合开发Web Service

    WebService之Axis2实例

    WebService之Axis2实例是Web服务开发中的一个重要环节,它基于SOAP协议,允许不同平台和语言间的应用程序进行通信。本文将详细介绍Axis2实例的创建、配置和运行,以及如何利用Eclipse IDE、JDK 1.7和Tomcat 7.0进行...

    Eclipse+Webservice简单开发实例

    【Eclipse+Webservice简单开发实例】是关于如何在Eclipse集成开发环境中使用Apache Axis2框架创建和测试Web服务的教程。本实例旨在帮助开发者理解Web服务的基本开发流程,特别是针对初学者,提供了详细的步骤指南。 ...

    axis2_WebService_开发指南

    【Axis2 WebService 开发指南】 Axis2 是 Apache 组织提供的一款强大的 WebService 开发框架,它在 Axis1 的基础上进行了很多优化和改进,提供了更高效、更灵活的服务发布和调用机制。本指南将深入讲解如何使用 ...

    axis2 webservice 实例

    总之,这个"axis2 Web service实例"是一个宝贵的教育资源,对于想要学习或提升在Java环境中使用Axis2开发Web服务的人来说非常有价值。通过这个实例,你可以深入理解Web服务的工作原理,熟悉Axis2的API使用,以及如何...

    经典入门:Axis2创建webservice.pdf

    Apache Axis2 是一个功能强大的 Web Service 开发框架,它提供了简单、高效的方法来构建和部署 Web Services。本教程将详细介绍如何使用 Axis2 创建 Web Services,从基础环境配置到实际服务的开发与部署。 ### 第...

    axis2 webService

    在描述中提到的“使用eclipse 插件 axis2 webService 实例”,是指利用Eclipse集成开发环境(IDE)中的Axis2插件来创建和测试Web服务的实践过程。Eclipse是一个广泛使用的Java IDE,其丰富的插件生态系统使得开发...

    axis 发布webservice的步骤

    为了更好地理解和实践这些步骤,可以参考给定的文档“Eclipse开发Axis_WebService实例总结_20100430.doc”。这个文档通常会包含详细的步骤、截图以及可能遇到的问题和解决方案,对于初学者非常有帮助。 总结,发布...

    基于axis2开发的webservice机soap的相关资料

    - "web_service技术--axis2在eclipse下开发指南"、"Axis2介绍和例子"、"axis2新手教程实例"等文档,详细介绍了在Eclipse环境下使用Axis2开发和部署Web Service的步骤和示例。 通过这些资料,开发者可以深入了解...

    Axis2_Codegen_Wizard_1.4.0( eclipse中Webservice客户端生成插件)

    Axis2 Codegen Wizard 1.4.0 是一个专门针对Eclipse集成开发环境设计的插件,用于简化Web服务客户端的生成过程。在Web服务的世界里,开发人员经常需要调用远程服务,以便通过网络进行数据交换。这个插件正是为了解决...

    用Java 开发 WebService Axis简单实例

    在开发之前, 先了解一下AXIS,axis目前出现的2版本以其灵活性,快速开发工具的集成高于xfire而使用者居其上.用Eclipse开发,一开始就会想到xfire,它是集成与Eclipse的.所以 开发起来非常方便,现在我们来看看axis的开发...

    axis2-webservice新手教程实例

    本教程将引导初学者通过实例了解如何使用 Axis2 开发 Web 服务。 1. **环境搭建** 首先,你需要准备以下工具: - JDK 1.6 或更高版本 - Tomcat 5.0 或更高版本 - MyEclipse 5.0 或其他支持的 IDE - 下载 Axis2...

    axis2+Tomcat5.5+Eclipse3.3

    【标题】:"axis2+Tomcat5.5+Eclipse3.3" 实例解析 【描述】:本文将深入探讨如何使用axis2、Tomcat5.5和Eclipse3.3来部署和访问Web服务,这是一份适用于初学者的学习指南。 【标签】:"webservice" "axis2" 在...

Global site tag (gtag.js) - Google Analytics