CXF对Interceptor拦截器的支持
前面在Axis中介绍过Axis的Handler,这里CXF的Interceptor就和Handler的功能类似。在每个请求响应之前或响应之后,做一些事情。这里的Interceptor就和Filter、Struts的Interceptor很类似,提供它的主要作用就是为了很好的降低代码的耦合性,提供代码的内聚性。下面我们就看看CXF的Interceptor是怎么样工作的。
1、 我们就用上面的HelloWorldService,客户端的调用代码重新写一份,代码如下:
package com.hoo.client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.phase.Phase;
import com.hoo.interceptor.MessageInterceptor;
import com.hoo.service.IHelloWorldService;
/**
* <b>function:</b>CXF WebService客户端调用代码
* @author hoojo
* @createDate 2011-3-16 上午09:03:49
* @file HelloWorldServiceClient.java
* @package com.hoo.client
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class ServiceMessageInterceperClient {
public static void main(String[] args) {
//调用WebService
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IHelloWorldService.class);
factory.setAddress("http://localhost:9000/helloWorld");
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
IHelloWorldService service = (IHelloWorldService) factory.create();
System.out.println("[result]" + service.sayHello("hoojo"));
}
}
上面的CXF的拦截器是添加在客户端,同样在服务器端也是可以添加拦截器Interceptor的。运行后结果如下:
2011-3-18 7:34:00 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.hoo.com/}IHelloWorldServiceService from class com.hoo.service.IHelloWorldService
2011-3-18 7:34:00 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
信息: Outbound Message
---------------------------
ID: 1
Address: http://localhost:9000/helloWorld
Encoding: UTF-8
Content-Type: text/xml
Headers: {SOAPAction=[""], Accept=[*/*]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHello xmlns:ns1="http://service.hoo.com/"><name>hoojo</name></ns1:sayHello></soap:Body></soap:Envelope>
--------------------------------------
2011-3-18 7:34:01 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], Content-Length=[230], Server=[Jetty(7.2.2.v20101205)]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHelloResponse xmlns:ns1="http://service.hoo.com/"><return>hoojo say: Hello World </return></ns1:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
[result]hoojo say: Hello World
上面的部分信息是LoggingInterceptor输出的日志信息,分别在请求和响应的时候输出日志信息,还有输出请求的时候参数的信息以及响应的时候返回值的信息。
2、 刚才是客户端添加Interceptor,现在我们自己编写一个Interceptor,这个Interceptor需要继承AbstractPhaseInterceptor,实现handleMessage和一个带参数的构造函数。然后在服务器端添加这个Interceptor。
Interceptor代码如下:
package com.hoo.interceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
/**
* <b>function:</b> 自定义消息拦截器
* @author hoojo
* @createDate Mar 17, 2011 8:10:49 PM
* @file MessageInterceptor.java
* @package com.hoo.interceptor
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class MessageInterceptor extends AbstractPhaseInterceptor<Message> {
//至少要一个带参的构造函数
public MessageInterceptor(String phase) {
super(phase);
}
public void handleMessage(Message message) throws Fault {
System.out.println("############handleMessage##########");
System.out.println(message);
if (message.getDestination() != null) {
System.out.println(message.getId() + "#" + message.getDestination().getMessageObserver());
}
if (message.getExchange() != null) {
System.out.println(message.getExchange().getInMessage() + "#" + message.getExchange().getInFaultMessage());
System.out.println(message.getExchange().getOutMessage() + "#" + message.getExchange().getOutFaultMessage());
}
}
}
下面看看发布服务和添加自定义拦截器的代码:
package com.hoo.service.deploy;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.phase.Phase;
import com.hoo.interceptor.MessageInterceptor;
import com.hoo.service.HelloWorldService;
/**
* <b>function:</b>在服务器发布自定义的Interceptor
* @author hoojo
* @createDate 2011-3-18 上午07:38:28
* @file DeployInterceptorService.java
* @package com.hoo.service.deploy
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class DeployInterceptorService {
public static void main(String[] args) throws InterruptedException {
//发布WebService
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
//设置Service Class
factory.setServiceClass(HelloWorldService.class);
factory.setAddress("http://localhost:9000/helloWorld");
//设置ServiceBean对象
factory.setServiceBean(new HelloWorldService());
//添加请求和响应的拦截器,Phase.RECEIVE只对In有效,Phase.SEND只对Out有效
factory.getInInterceptors().add(new MessageInterceptor(Phase.RECEIVE));
factory.getOutInterceptors().add(new MessageInterceptor(Phase.SEND));
factory.create();
System.out.println("Server start ......");
Thread.sleep(1000 * 60);
System.exit(0);
System.out.println("Server exit ");
}
}
值得说的是,以前发布WebService是用Endpoint的push方法。这里用的是JaxWsServerFactoryBean和客户端调用的代码JaxWsProxyFactoryBean有点不同。
客户端调用代码:
package com.hoo.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.hoo.service.IHelloWorldService;
/**
* <b>function:</b>CXF WebService客户端调用代码
* @author hoojo
* @createDate 2011-3-16 上午09:03:49
* @file HelloWorldServiceClient.java
* @package com.hoo.client
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class HelloWorldServiceClient {
public static void main(String[] args) {
//调用WebService
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IHelloWorldService.class);
factory.setAddress("http://localhost:9000/helloWorld");
IHelloWorldService service = (IHelloWorldService) factory.create();
System.out.println("[result]" + service.sayHello("hoojo"));
}
}
CXF对Interceptor拦截器的支持的更多相关文章
- CXF实战之拦截器Interceptor(四)
拦截器(Interceptor)是CXF功能最基本的扩展点,能够在不正确核心模块进行改动的情况下.动态加入非常多功能.拦截器和JAX-WS Handler.Filter的功能相似,当服务被调用时.就会 ...
- SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- Spring MVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- SpringMVC 中的Interceptor 拦截器
1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors> <!-- 日志拦截器 --> <mvc:interceptor> ...
- SpringMVC中的Interceptor拦截器及与Filter区别
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- [转]SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- SpringMVC之七:SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- Spring中的Interceptor 拦截器 专题
spring-webmvc-4.3.14.RELEASE.jar org.springframework.web.servlet.DispatcherServlet#doDispatch /** * ...
- SpringMvc中Interceptor拦截器用法
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆等. 一. 使用场景 1 ...
随机推荐
- MinGW下简单编译FFmpeg
2009.03.21补充:ffmpeg-0.5正式发布,地址为:[url]http://www.ffmpeg.org/releases/ffmpeg-0.5.tar.bz2[/url].修改了第7步, ...
- VS2010 如何添加H文件目录和LIB目录
第一次使用VS2010,也是初学者开始编写VC++,程序首先学习编写DLL文件,编译完自己的DLL文件后,要在其它项目中使用,开始遇到很多错,但是在网上搜索了好久后,终于解决了问题. H文件目录: 依 ...
- 大数据处理的三种框架:Storm,Spark和Samza
许多分布式计算系统都可以实时或接近实时地处理大数据流.下面对三种Apache框架分别进行简单介绍,然后尝试快速.高度概述其异同. Apache Storm 在Storm中,先要设计一个用于实时计算的图 ...
- Windows--常见端口号
windows--常见端口号 此文档仅供参考,相关端口作用以国际标准为准. 端口:0 服务:Reserved 说明:通常用于分析操作系统.这一方法能够工作是因为在一些系统中"0" ...
- hdu3081 Marriage Match II
新年第一篇,又花了一早上,真是蠢啊! 二分+网络流 之前对于讨论哪些人是朋友的时候复杂度过高 直接n3的暴力虽然看起来复杂度高,其实并不是每次都成立 #include<bits/stdc++.h ...
- class-支持向量机SVM全析笔记
support vector machines,SVM是二类分类模型.定义在特征空间上间隔最大的线性分类器,由于包括核技巧实质上成为非线性分类器.学习策略是间隔最大化,可形式化为求解凸二次规划问题(c ...
- Alice and Bob HDU - 4268
Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N ...
- freemarker中的substring取子串(十四)
freemarker中的substring取子串 1.substring取子串介绍 (1)表达式?substring(from,to) (2)当to为空时,默认的是字符串的长度 (3)from是第一个 ...
- jquery初始化的三种方式
第一种 $(document).ready(function(){ alert("第一种方法."); }); 第二种 $(function(){ alert("第二种方法 ...
- 【NOIP2006】能量项链
题面 Description 在 Mars 星球上,每个 Mars 人都随身佩带着一串能量项链.在项链上有 N 颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的 ...