使用Eclipse+Axis2构建Web Service应用(客户端无需生成Stub方式)
来源:互联网 发布:淘宝特惠囤 编辑:程序博客网 时间:2024/06/05 17:45
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://panpan.blog.51cto.com/489034/119204
本文介绍了如何使用tomcat和Axis2开发、部署和使用Web Service,并且开发了一个简单的例子,通过例子开发流程来向大家展示如何创建Axis2的服务端和客户端。该例子与其他例子最大的不同是不是用客户端stub存根方式调用web服务,提供了一种统一的客户端调用方式。希望读者读完本文后能够快速上手Axis2的应用开发。
一、开发环境和软件下载
开发环境是:Eclipse3.4 + JDK1.6 + Tomcat6.0 + Axis2 1.3。这些软件都是各个软件最新版本,下载方式由于软件网站的更新,地址也会更新,这里就不再贴出来了,就请读者到相应网站下载。
软件下载和安装:
(1) JDK1.6环境变量配置:
JAVA_HOME: C:\Program Files\Java\jdk1.6.0_07,其中C:\Program Files\Java是我的JDK安装目录。
Path: %JAVA_HOME%\bin
Classpath: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
(2) tomcat6.0环境变量配置:
CATALINA_HOME:C:\Program Files\Apache Software Foundation\Tomcat 6.0, 其中C:\Program Files\Apache Software Foundation是我的Tomcat 6.0安装目录。
其实这里tomcat6.0不需要配置环境变量,因为最新版本不需要配置。
(3) Axis2 1.3下载:从[url]http://apache.justdn.org/ws/axis2/1_4_1/[/url]网站上下载axis2-1.3-war.zip 。将其解压,把其中的axis2.war文件复制到tomcat的webapps目录下即可完成Axis2的安装。
安装完成后,启动tomcate,在地址栏内输入[url]http://localhost:8080/axis2/[/url],打开如下图界面,表明安装正确:
(4) Eclipse3.4下载:到[url]www.eclipse.org[/url]官方网站下载,直接解压后配置Windows/Preference中的JDK即可。
(5) 下载Eclipse Axis2插件:axis2-eclipse-codegen-wizard-1.4.zip和axis2-eclipse-codegen-wizard-1.4.zip。下载地址是:[url]http://apache.justdn.org/ws/axis2/tools/1_4_1/ [/url] 这两个插件解压到Eclipse安装目录下plugins中即可。打开Eclipse,选择File/New/Other菜单项,看到如下界面表明安装成功:
安装这两个插件的目的是:方便生成Axis2服务和生成Axis2客户端,这里我使用了无Stub方式的客户端调用服务,因此就不演示生成Axis2客户端的功能了。
二、构建服务
(1)在 Eclispse 中添加一个用户库命名为 axis2 ,将axis2\lib 下的包均添加进来。这一步操作的目的是为了以后工程添加axis2 所需的 jar 文件方便。
(2) 建立一个JavaProject 命名为 ws ,将 axis2 用户库加入到 build path 中。
(3) 现在开始编写要发布的 WebSevice ,在 src中建包 briup ,建立 Hello 类如下:
2.发布Web Service
打包要发布的 Service ,Eclipse 中New --> File --> Other --> Axis2 wizards --> Axis2 Services Archiver,按照向导选择刚建立的类编译后的class 文件。
(1)选择class 文件目录,注意,不是java 源文件,而是classes 目录,这里需要注意由于你的类是带包briup的,因此不要选到包这个目录。
一、开发环境和软件下载
开发环境是:Eclipse3.4 + JDK1.6 + Tomcat6.0 + Axis2 1.3。这些软件都是各个软件最新版本,下载方式由于软件网站的更新,地址也会更新,这里就不再贴出来了,就请读者到相应网站下载。
软件下载和安装:
(1) JDK1.6环境变量配置:
JAVA_HOME: C:\Program Files\Java\jdk1.6.0_07,其中C:\Program Files\Java是我的JDK安装目录。
Path: %JAVA_HOME%\bin
Classpath: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
(2) tomcat6.0环境变量配置:
CATALINA_HOME:C:\Program Files\Apache Software Foundation\Tomcat 6.0, 其中C:\Program Files\Apache Software Foundation是我的Tomcat 6.0安装目录。
其实这里tomcat6.0不需要配置环境变量,因为最新版本不需要配置。
(3) Axis2 1.3下载:从[url]http://apache.justdn.org/ws/axis2/1_4_1/[/url]网站上下载axis2-1.3-war.zip 。将其解压,把其中的axis2.war文件复制到tomcat的webapps目录下即可完成Axis2的安装。
安装完成后,启动tomcate,在地址栏内输入[url]http://localhost:8080/axis2/[/url],打开如下图界面,表明安装正确:
(4) Eclipse3.4下载:到[url]www.eclipse.org[/url]官方网站下载,直接解压后配置Windows/Preference中的JDK即可。
(5) 下载Eclipse Axis2插件:axis2-eclipse-codegen-wizard-1.4.zip和axis2-eclipse-codegen-wizard-1.4.zip。下载地址是:[url]http://apache.justdn.org/ws/axis2/tools/1_4_1/ [/url] 这两个插件解压到Eclipse安装目录下plugins中即可。打开Eclipse,选择File/New/Other菜单项,看到如下界面表明安装成功:
安装这两个插件的目的是:方便生成Axis2服务和生成Axis2客户端,这里我使用了无Stub方式的客户端调用服务,因此就不演示生成Axis2客户端的功能了。
二、构建服务
在开发环境及Axis2 环境搭建好后,我们便可着手 Web Services服务的开发:
1.建立要发布的Web Service
(1)在 Eclispse 中添加一个用户库命名为 axis2 ,将axis2\lib 下的包均添加进来。这一步操作的目的是为了以后工程添加axis2 所需的 jar 文件方便。
(2) 建立一个JavaProject 命名为 ws ,将 axis2 用户库加入到 build path 中。
(3) 现在开始编写要发布的 WebSevice ,在 src中建包 briup ,建立 Hello 类如下:
package briup;
public class Hello {
public String sayHello(String user) {
return "Hello, " + user;
}
}
public class Hello {
public String sayHello(String user) {
return "Hello, " + user;
}
}
2.发布Web Service
打包要发布的 Service ,Eclipse 中New --> File --> Other --> Axis2 wizards --> Axis2 Services Archiver,按照向导选择刚建立的类编译后的class 文件。
(1)选择class 文件目录,注意,不是java 源文件,而是classes 目录,这里需要注意由于你的类是带包briup的,因此不要选到包这个目录。
(2)连按两次“Next>” ,选中 “Generate the service xml automatically”
(3)按下一步,输入service 名称和类名,我这里输入的服务名是:ws;类名是我们刚刚写的类名:briup.Hello,这里需要注意加入完整的包名。
(4)按下一步,输入 service 文件的保存路径和文件名,完成。
选择生成目录为:C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\axis2\WEB-INF\services,也可以选择其他目录,然后copy到这一目录下。
3.测试Web Service
打开[url]http://localhost:8080/axis2/services/listServices[/url]页面,可以看到ws服务,点击进到ws的wsdl页面:[url]http://localhost:8080/axis2/services/ws?wsdl[/url],表明服务部署正确。
三、编写客户端代码调用服务
我的这个例子与其他例子最大的不同就在这里,其他例子一般需要根据刚才的服务wsdl生成客户端stub,然后通过stub来调用服务,这种方式显得比较单一,客户端必须需要stub存根才能够访问服务,很不方面。
本例子的客户端不采用stub方式,而是一种实现通用的调用方式,不需要任何客户端存根即可访问服务。只需要指定对于的web servce地址、操作名、参数和函数返回类型即可。代码如下:
package briup;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class WsClient {
private RPCServiceClient serviceClient;
private Options options;
private EndpointReference targetEPR;
public WsClient(String endpoint)throws AxisFault {
serviceClient = new RPCServiceClient();
options = serviceClient.getOptions();
targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
}
public Object[] invokeOp(String targetNamespace, String opName,
Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
ClassNotFoundException {
// 设定操作的名称
QName opQName = new QName(targetNamespace, opName);
// 设定返回值
//Class<?>[] opReturn = new Class[] { opReturnType };
// 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
}
/**
* @param args
* @throws AxisFault
* @throws ClassNotFoundException
*/
public static void main(String[] args)throws AxisFault, ClassNotFoundException {
// TODO Auto-generated method stub
final String endPointReference ="http://localhost:8080/axis2/services/ws";
final String targetNamespace ="http://briup";
WsClient client = new WsClient(endPointReference);
String opName = "sayHello";
Object[] opArgs = new Object[]{"Repace中心"};
Class<?>[] opReturnType =new Class[]{String[].class};
Object[] response = client.invokeOp(targetNamespace, opName, opArgs, opReturnType);
System.out.println(((String[])response[0])[0]);
}
}
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class WsClient {
private RPCServiceClient serviceClient;
private Options options;
private EndpointReference targetEPR;
public WsClient(String endpoint)throws AxisFault {
serviceClient = new RPCServiceClient();
options = serviceClient.getOptions();
targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
}
public Object[] invokeOp(String targetNamespace, String opName,
Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
ClassNotFoundException {
// 设定操作的名称
QName opQName = new QName(targetNamespace, opName);
// 设定返回值
//Class<?>[] opReturn = new Class[] { opReturnType };
// 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
}
/**
* @param args
* @throws AxisFault
* @throws ClassNotFoundException
*/
public static void main(String[] args)throws AxisFault, ClassNotFoundException {
// TODO Auto-generated method stub
final String endPointReference ="http://localhost:8080/axis2/services/ws";
final String targetNamespace ="http://briup";
WsClient client = new WsClient(endPointReference);
String opName = "sayHello";
Object[] opArgs = new Object[]{"Repace中心"};
Class<?>[] opReturnType =new Class[]{String[].class};
Object[] response = client.invokeOp(targetNamespace, opName, opArgs, opReturnType);
System.out.println(((String[])response[0])[0]);
}
}
运行该程序,点击Run As->Java application,可以看到控制台端口的输出是:Hello, Repace中心 表明客户端调用成功。
该例子最大的不同和优势表现在客户端的调用方式,或者说是发起服务调用的方式,虽然比起客户端stub存根的方式,代码稍多,但是这种方式统一,不需要生产stub存根代码,解决了客户端有很多类的问题。如果读者对这些代码进一步封装,我想调用方式很简单,只需要传递相关参数,这更好地说明了服务调用的优势。而且这种方式更加简单明了,一看便知具体含义。而不需要弄得stub类的一些机制。
本文出自 “C++技术” 博客,请务必保留此出处http://panpan.blog.51cto.com/489034/119204
0 0
- 使用 Eclipse+Axis2 构建 Web Service 应用(客户端无需生成 Stub 方式)
- 使用Eclipse+Axis2构建Web Service应用(客户端无需生成Stub方式)
- WebServices学习2——使用Eclipse+Axis2构建Web Service应用(客户端无需生成Stub方式)
- Axis2客户端Web Service调用(客户端无需生成Stub方式)
- Axis2构建Web Service应用(无需Stub方式)
- 使用Axis2来构建Web Service客户端
- Eclipse中Axis2 Stub开发Web Service
- 在Eclipse中使用Axis2插件生成Web Service服务端/客户端
- 在Eclipse中使用Axis2插件生成Web Service服务端/客户端
- 在Eclipse中使用Axis2插件生成Web Service服务端/客户端
- 采用axis2方式Java客户端调用WCF服务端Web Service
- AXIS2生成Stub类
- 使用Eclipse自带Web Service插件(Axis1.4)生成Web Service服务端/客户端
- 实战Eclipse生成Web Service客户端
- 使用Axis2创建一个Web Service的客户端
- 使用Axis2构建REST Service
- [Axis2与Eclipse整合开发Web Service系列之二] Top-Down方式,通过WSDL逆向生成服务端
- [Axis2与Eclipse整合开发Web Service系列之二] Top-Down方式,通过WSDL逆向生成服务端(续)
- The Blocks Problem
- Android的图形与图像处理之四 补间动画(Tween)
- 使用Jenkins进行持续集成(一)-安装篇
- 工作中常用到的linux命令
- 第八周项目六--本月天数
- 使用Eclipse+Axis2构建Web Service应用(客户端无需生成Stub方式)
- Android的图形与图像处理之五 属性动画
- EL表达式(1)
- 学生作品(仿QQ2014)
- Android的图形与图像处理之六 SurfaceView实现动画
- Android学习笔记--网络图片查看器和网页源码查看器
- Android数据存储与IO之一 SharedPreference与File存储
- Uva 3027 Corporative Network
- Ubuntu12.04密码正确无法登陆