axis2开发webservice之编写Axis2模块(Module)
axis2中的模块化开发。能够让开发者自由的加入自己所需的模块。提高开发效率,减少开发的难度。
Axis2能够通过模块(Module)进行扩展。
Axis2模块至少须要有两个类,这两个类分别实现了Module和Handler接口。开发和使用一个Axis2模块的过程例如以下:
1. 编写实现Module接口的类。
Axis2模块在进行初始化、销毁等动作时会调用该类中对应的方法)。
2. 编写实现Handler接口的类。该类是Axis2模块的业务处理类。
3. 编写module.xml文件。该文件放在META-INF文件夹中。用于配置Axis2模块。
4. 在axis2.xml文件里配置Axis2模块。
5. 在services.xml文件里配置Axis2模块。每个Axis2模块都须要使用<module>元素引用才干使用。
6. 公布Axis2模块。须要使用jar命令将Axis2模块压缩成.mar包(文件扩展名必须是.mar),然后将.mar文件放在<Tomcat安装文件夹>\webapps\axis2\WEB-INF\modules文件夹中。
先来编写一个WebService类,代码例如以下:
package ws; public class TestWs { public String showName(String name) {
return name;
} public String getName() {
return "axis2 webservice";
}
}
以下我们来编写一个记录请求和响应SOAP消息的Axis2模块。当client调用WebService方法时,该Axis2模块会将请求和响应SOAP消息输出到Tomcat控制台上。
第1步:编写LoggingModule类
LoggingModule类实现了Module接口。代码例如以下:
package module; import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisDescription;
import org.apache.axis2.description.AxisModule;
import org.apache.axis2.modules.Module;
import org.apache.neethi.Assertion;
import org.apache.neethi.Policy; public class LoggingModule implements Module {
public void applyPolicy(Policy arg0, AxisDescription arg1) throws AxisFault {
} public boolean canSupportAssertion(Assertion arg0) {
return true;
} public void engageNotify(AxisDescription arg0) throws AxisFault {
} public void init(ConfigurationContext arg0, AxisModule arg1)
throws AxisFault {
System.out.println("init");
} public void shutdown(ConfigurationContext arg0) throws AxisFault {
System.out.println("shutdown");
}
}
在本例中LoggingModule类并没实现实际的功能。但该类必须存在。
当Tomcat启动时会装载该Axis2模块。同一时候会调用LoggingModule类的init方法。并在Tomcat控制台中输出“init”。
第2步:编写LogHandler类
LogHandler类实现了Handler接口。代码例如以下:
package module; import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler; public class LogHandler extends AbstractHandler implements Handler {
private String name; public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Handler.InvocationResponse invoke(MessageContext arg0)
throws AxisFault {
System.out.println(arg0.getEnvelope().toString());
return Handler.InvocationResponse.CONTINUE;
} public void revoke(MessageContext msgContext) {
System.out.println(msgContext.getEnvelope().toString());
}
}
LogHandler类的核心方法是invoke,当使用该Axis2模块的WebService的方法被调用时。LogHandler类的invoke方法被调用。
第3步:编写module.xml文件
在META-INF文件夹中建立一个module.xml文件,内容例如以下:
<module name="logging" class="module.LoggingModule">
<InFlow>
<handler name="InFlowLogHandler" class="module.LogHandler">
<order phase="loggingPhase"/>
</handler>
</InFlow>
<OutFlow>
<handler name="OutFlowLogHandler" class="module.LogHandler">
<order phase="loggingPhase"/>
</handler>
</OutFlow> <OutFaultFlow>
<handler name="FaultOutFlowLogHandler" class="module.LogHandler">
<order phase="loggingPhase"/>
</handler>
</OutFaultFlow>
<InFaultFlow>
<handler name="FaultInFlowLogHandler" class="module.LogHandler">
<order phase="loggingPhase"/>
</handler>
</InFaultFlow>
</module>
第4步:在axis2.xml文件里配置Axis2模块
打开axis2.xml(web-inf/conf)文件。分别在例如以下四个<phaseOrder>元素中增加<phase
name="loggingPhase"/>:
<phaseOrder type="InFlow"> <phase name="soapmonitorPhase"/>
<phase name="loggingPhase"/>
</phaseOrder>
<phaseOrder type="OutFlow"> <phase name="Security"/>
<phase name="loggingPhase"/>
</phaseOrder>
<phaseOrder type="InFaultFlow"> <phase name="soapmonitorPhase"/>
<phase name="loggingPhase"/>
</phaseOrder>
<phaseOrder type="OutFaultFlow"> <phase name="Security"/>
<phase name="loggingPhase"/>
</phaseOrder>
第5步:在services.xml文件里引用logging模块
services.xml文件的内容例如以下:
<service name="AxisService">
<description>AxisService</description>
<parameter name="ServiceClass">ws.TestWs</parameter>
<module ref="logging"/>
<operation name="showName">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getName">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</operation>
</service>
第6步:公布logging模块
到如今为止,我们应用能够建立两个发行包:logging.mar和service.aar。跟我们之前公布的.aar包的过程是一样的。当中logging.mar文件是Axis2模块的发行包。该包的文件夹结构例如以下:
logging.mar
module\LoggingModule.class
module\LogHandler.class
META-INF\module.xml
service.aar文件是本例编写的WebService发行包。该包的文件夹结构例如以下:
service.aar
service\MyService.class
META-INF\services.xml
将logging.mar文件放在<Tomcat安装文件夹>\webapps\axis2\WEB-INF\modules文件夹中,将service.aar文件放在<Tomcat安装文件夹>\webapps\axis2\WEB-INF\services文件夹中。要注意的是,假设modules文件夹中包括了modules.list文件,Axis2会仅仅装载在该文件里引用的Axis2模块。因此,必须在该文件里引用logging模块,该文件的内容例如以下:
假设modules文件夹中不包括modules.list文件,则Axis2会装载modules文件里的全部Axis2模块。
如今启动Tomcat,结果例如以下
能够看到,logging已经初始化了。
接下来就是用wsdl2java方法生成client代码,再去调用webservice,例如以下
package ws; import java.rmi.RemoteException; import org.apache.axis2.AxisFault; public class TestClient { <span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>AxisServiceStub stub = new AxisServiceStub();
<span style="white-space:pre"> </span>ShowName show = new ShowName();
<span style="white-space:pre"> </span>show.setName("thinkpad,今天任务完毕的不错,加油!");
<span style="white-space:pre"> </span>System.out.println(stub.showName(show).get_return());
<span style="white-space:pre"> </span>} catch (AxisFault e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>} catch (RemoteException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
在这里须要强调的一点就是,System.out.println(stub.showName(show).get_return());这里一定要get_return()一下,尽管在java se中,这时不须要的。可是这里是webservice。是须要从server端来获取。否则的话,打印出来的就是返回值的内存地址。假设不注意的话,非常easy觉得是没有重写toString()方法的原因。
执行结果
可是始终没有在控制台输出对应的请求和响应SOAP消息。没办法,仅仅好再细致检查了一遍,没发现错误,还以为是tomcat须要重新启动一下才干够呢。结果在stop时,发现tomcat输出了soap信息,例如以下
完整的结果例如以下
axis2开发webservice之编写Axis2模块(Module)的更多相关文章
- Axis2开发webservice详解
Axis2开发webservice详解 标签: javawebserviceAxis2 2015-08-10 10:58 1827人阅读 评论(0) 收藏 举报 分类: JAVA(275) 服务器 ...
- Axis2开发WebService客户端 的3种方式
Axis2开发WebService客户端 的3种方式 在dos命令下 wsdl2java -uri wsdl的地址(网络上或者本地) -p com.whir.ezoffi ...
- axis2开发webservice接口入门到精通详解(转)
最近在开发接口,在网上发现了两篇不错的文章,给大家分享下: 第一篇: 一.Axis2的下载和安装 1.可从http://ws.apache.org/axis2/ 下载Axis2的最新版本: ...
- axis2开发webservice程序
一.环境 eclipse + jdk 6.0 + win7 64位 +tomcat7.0 二.创建服务端程序 1.新建web项目,webserviceTest 2.下载axis2,将lib目录下的ja ...
- axis2开发webservice总结
需求环境:对接方公司提供wsdl文件,我方按照该wsdl文件开发服务端. 配置axis2开发环境,网上教程很多,不再啰嗦.环境搭好后执行wsdl2java -uri file:///C:/Users/ ...
- axis2开发webservice入门到精通
1,准备工作: 首先我们要下载:axis2-1.4.1-war(发布webservice),axis2-1.4.1-bin.zip(webservice调用使用的各种包). 下载好了,把axis2-1 ...
- CXF和Axis2开发webservice也是可以添加asmx等后缀
在当家互联网时代, 手机APP所需要的后台服务接口经常会变化, 如果前期没有设计好, 把它们的请求地址配置在比较稳定不会经常修改的地址(例如专门一个后台服务用于获取所有最新的数据服务地址)这样不会因为 ...
- 使用Axis2开发WebService
一.准备 1.下载Axis2.eclipse插件 axis2-1.6.2-war.zip: http://mirror.bjtu.edu.cn/apache//axis/axis2/java/core ...
- Spring注解+Axis2开发WebService
用Spring注解方式: 配置扫描指定包下的类 <context:component-scan base-package="包名" /> 标识类为spring管理的 ...
随机推荐
- 32位和64位系统下 int、char、long、double所占的内存
32位和64位系统下 int.char.long.double所占内存
- [bzoj4899]记忆的轮廓 题解(毒瘤概率dp)
题目背景 四次死亡轮回后,昴终于到达了贤者之塔,当代贤者夏乌拉一见到昴就上前抱住了昴“师傅!你终于回来了!你有着和师傅一样的魔女的余香,肯定是师傅”.众所周知,大贤者是嫉妒魔女沙提拉的老公,400年前 ...
- 使用html2canvas实现网页截图,并嵌入到PDF
使用html2canvas实现网页截图并嵌入到PDF 以前我们只能通过截图工具进行截取图像.这使得在业务生产中,变得越来越不方便.目前的浏览器功能越来越强大,H5也逐渐普及,浏览器也可以实现截图了.这 ...
- 洛谷——P1627 [CQOI2009]中位数
P1627 [CQOI2009]中位数 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 中位数的题目有关统计的话,可以转 ...
- MySQL存储过程实践
对employees数据库建立存储过程 创建不含有输入输出变量的存储过程 DELIMITER // -- 设定语句结束分隔符 DROP PROCEDURE IF EXISTS GetEmployees ...
- kvm客户机存储方式
前面介绍了存储的配置和qemu-img工具来管理镜像,在QEMU/KVM中,客户机镜像文件可以由很多种方式来构建,其中几种如下: 1) 本地存储的客户机镜像文件. 2) 物理磁盘或磁盘分区. 3) L ...
- git/github初级使用
1.常见的github 国内最流行的php开发框架(thinkphp):https://github.com/top-think/thinkphp 全球最流行的php框架(laravel):https ...
- LeetCode728. 自除数
自除数 是指可以被它包含的每一位数除尽的数. 例如,128 是一个自除数,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0. 还有,自除数不允许包含 0 . 给定上边 ...
- C语言 NULL 是什么鬼
NULL , 0 , '\0' 之间的区别与联系 1.NULL 结构体的使用中,都可以用NULL表示空,那么NULL是什么 #ifndef __cplusplus #define NULL ((vo ...
- 理解js的几个关键问题(1):全局变量new和关于hasOwnPropery和PropertyIsEnumerable 等
一.作用域和全局变量 var test=function(){ var a=1; setTimeout(function(){ console.log(a); a=2; },1000); a=3; s ...