拦截器是Cxf的基础,Cxf中很多的功能都是由内置的拦截器来实现的,拦截器在Cxf中由Interceptor表示。拦截器的作用类似axis2中handle。Cxf的拦截器包括入拦截器和出拦截器,所有的入拦截器或出拦截器构成了一个拦截器链,它们可以作用在Server端也可以作用在Client端。当需要使用拦截器链处理消息的时候,Cxf会确保对应的消息被拦截器链里面的每一个拦截器都执行一遍。拦截器链在Cxf中由InterceptorChain接口表示,org.apache.cxf.phase.PhaseInterceptorChain类的public synchronized boolean doIntercept(Message message)方法,它是拦截器链拦截Message的实现。当客户端发送一个请求到服务端时会经过客户端的出拦截器和服务端的入拦截器;当服务端对客户端的请求进行响应时对应的响应消息会经过服务端的出拦截器和客户端的入拦截器。

下面简单介绍一下服务端拦截器,在上一篇工程基础上进行如下修改:

在服务端应用里面,增加一个类,这个类继承自AbstractPhaseInterceptor。

package com.myl.interceptor;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
/**
*
* @author myl
* @date 2018年4月18日 上午12:10:20
*
* 拦截器,继承AbstractPhaseInterceptor<Message>
*/
public class WsInterceptor extends AbstractPhaseInterceptor<Message> { public WsInterceptor(String phase) {
super(phase);
// TODO Auto-generated constructor stub
} @Override
public void handleMessage(Message message) throws Fault {
// TODO Auto-generated method stub
System.out.println("-------into Interceptor-------");
System.out.println(message);
if(message.getDestination() != null){
System.out.println(message.getId() + "-" + message.getDestination().getMessageObserver());
}
if(message.getExchange() != null){
System.out.println(message.getId() + "#" + message.getExchange().getInFaultMessage());
System.out.println(message.getId() + "#" + message.getExchange().getOutFaultMessage());
}
System.out.println("-------out Interceptor-------");
} }

修改服务发布,注意下面加粗斜体的地方

package com.myl.test.publish;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.phase.Phase; import com.myl.interceptor.WsInterceptor;
import com.myl.service.serviceImpl.StudentWsImpl; /**
*
* @author myl
* @date 2018年4月15日 下午11:26:50
* 服务端发布接口
*/
public class StudentTest { public static void main(String[] args) {
System.out.println("web service start");
//获取工厂对象
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); //这里必须传入class,不能是接口,否则客户端会报异常 Could not instantiate service class ... because it is an interface
factory.setServiceClass(StudentWsImpl.class);
//传入接口url
factory.setAddress("http://localhost:5555/studentws");
//往入拦截器链中添加拦截器
factory.getInInterceptors().add(new WsInterceptor(Phase.RECEIVE));
//往出拦截器链中添加拦截器
factory.getOutInterceptors().add(new WsInterceptor(Phase.SEND));
//创建接口
Server server = factory.create();
//启动接口
server.start();
System.out.println("web service started");
} }

启动服务后,当客户端调用服务端接口的时候,我们可以看到拦截器发挥作用,日志如下:

-------into Interceptor-------
{http.base.path=http://localhost:5555, HTTP.REQUEST=Request(POST /studentws?wsdl)@4b47c90, org.apache.cxf.transport.Destination=org.apache.cxf.transport.http_jetty.JettyHTTPDestination@4561dae, HTTP.CONFIG=null, org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@6da4eb38, org.apache.cxf.message.Message.QUERY_STRING=wsdl, org.apache.cxf.message.Message.ENCODING=UTF-8, HTTP.CONTEXT=ServletContext@o.e.j.s.h.ContextHandler@5082d622{/,null,AVAILABLE}, Content-Type=text/xml; charset=UTF-8, org.apache.cxf.security.SecurityContext=org.apache.cxf.transport.http.AbstractHTTPDestination$2@12c06064, org.apache.cxf.continuations.ContinuationProvider=org.apache.cxf.transport.http.Servlet3ContinuationProvider@188a71a, org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[250], content-type=[text/xml; charset=UTF-8], Host=[localhost:5555], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache-CXF/3.1.15]}, org.apache.cxf.request.url=http://localhost:5555/studentws, Accept=*/*, org.apache.cxf.request.uri=/studentws, org.apache.cxf.message.Message.PATH_INFO=/studentws, org.apache.cxf.transport.https.CertConstraints=null, HTTP.RESPONSE=HTTP/1.1 200
Date: Tue, 17 Apr 2018 16:24:13 GMT , org.apache.cxf.request.method=POST, org.apache.cxf.async.post.response.dispatch=true, org.apache.cxf.message.Message.IN_INTERCEPTORS=[org.apache.cxf.transport.https.CertConstraintsInterceptor@fd5a77f], HTTP_CONTEXT_MATCH_STRATEGY=stem, http.service.redirection=null, org.apache.cxf.message.Message.BASE_PATH=/studentws, org.apache.cxf.configuration.security.AuthorizationPolicy=null, org.apache.cxf.message.Message.FIXED_PARAMETER_ORDER=false}
null-org.apache.cxf.transport.ChainInitiationObserver@45e27096
null#null
null#null
-------out Interceptor-------
addStudent-------------
student是否为空--com.myl.entity.Student@490b0e79
ya -------into Interceptor-------
{org.apache.cxf.message.Message.PROTOCOL_HEADERS={}, http.headers.copied=true, org.apache.cxf.service.model.MessageInfo=[MessageInfo OUTPUT: {http://serviceImpl.service.myl.com/}addStudentServiceResponse], org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@6da4eb38, javax.xml.ws.wsdl.operation={http://serviceImpl.service.myl.com/}addStudentService, javax.xml.ws.wsdl.service={http://serviceImpl.service.myl.com/}StudentWsImplService, HTTP.RESPONSE=HTTP/1.1 200
Date: Tue, 17 Apr 2018 16:24:13 GMT
Content-Type: text/xml; charset=UTF-8 , org.apache.cxf.headers.Header.list=[], schema-validation-enabled=NONE, wrote.envelope.start=true, org.apache.cxf.service.model.BindingMessageInfo=org.apache.cxf.service.model.BindingMessageInfo@1603131c, javax.xml.ws.wsdl.port={http://serviceImpl.service.myl.com/}StudentWsImplPort, javax.xml.ws.wsdl.interface={http://serviceImpl.service.myl.com/}StudentWsImpl, javax.xml.ws.wsdl.description=http://localhost:5555/studentws?wsdl, org.apache.cxf.mime.headers={}, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.ws.policy.EffectivePolicy=org.apache.cxf.ws.policy.EffectivePolicyImpl@2d1abacf, org.apache.cxf.message.Message.RESPONSE_CODE=200, Content-Type=text/xml}
null#null
null#null
-------out Interceptor-------
省略部分....

客户端可以跟服务端类似写,我测试直接用的一样的

package com.interceptor;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
/**
*
* @author myl
* @date 2018年4月18日 上午12:10:20
*
* 客户端 拦截器,继承AbstractPhaseInterceptor<Message>
*/
public class WsInterceptor extends AbstractPhaseInterceptor<Message> { public WsInterceptor(String phase) {
super(phase);
// TODO Auto-generated constructor stub
} @Override
public void handleMessage(Message message) throws Fault {
// TODO Auto-generated method stub
System.out.println("-------into Interceptor Client-------");
System.out.println(message);
if(message.getDestination() != null){
System.out.println(message.getId() + "-" + message.getDestination().getMessageObserver());
}
if(message.getExchange() != null){
System.out.println(message.getId() + "#" + message.getExchange().getInFaultMessage());
System.out.println(message.getId() + "#" + message.getExchange().getOutFaultMessage());
}
System.out.println("-------out Interceptor Client-------");
} }

客户端调用接口

package com.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.phase.Phase; import com.interceptor.WsInterceptor;
import com.webservice.StudentWsImpl; /**
* @author myl
* @date 2018年4月15日
* 客户端调用服务端发布的接口
*/
public class StudentSerivceTest { public static void main(String[] args) {
//获取工厂对象
JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();
//加载生成的StudentWsImpl类
jwpfb.setServiceClass(StudentWsImpl.class);
//传入url接口地址
jwpfb.setAddress("http://localhost:5555/studentws?wsdl");
//往入拦截器链中添加拦截器
jwpfb.getInInterceptors().add(new WsInterceptor(Phase.RECEIVE));
//往出拦截器链中添加拦截器
jwpfb.getOutInterceptors().add(new WsInterceptor(Phase.SEND)); //创建接口对象
StudentWsImpl ws = (StudentWsImpl) jwpfb.create();
//调用接口中的方法
ws.addStudentService("mao", "11", "22");
ws.addStudentService("ya", "15", "23");
ws.queryStudentService("mao");
ws.queryStudentService("ya"); } }

客户端日志

-------into Interceptor Client-------
{org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[*/*], SOAPAction=[""]}, org.apache.cxf.transport.Conduit=conduit: class org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit57241990target: http://localhost:5555/studentws?wsdl, use.async.http.conduit=false, org.apache.cxf.service.model.MessageInfo=[MessageInfo INPUT: {http://serviceImpl.service.myl.com/}addStudentService], org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@1583741e, org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://localhost:5555/studentws?wsdl, org.apache.cxf.headers.Header.list=[], schema-validation-enabled=NONE, org.apache.cxf.request.method=POST, http.connection.address=org.apache.cxf.transport.http.Address@5b367418, wrote.envelope.start=true, http.connection=sun.net.www.protocol.http.HttpURLConnection:http://localhost:5555/studentws?wsdl, org.apache.cxf.service.model.BindingMessageInfo=org.apache.cxf.service.model.BindingMessageInfo@36060e, org.apache.cxf.invocation.context={ResponseContext={}, RequestContext={org.apache.cxf.jaxws.context.WrappedMessageContext.SCOPES={org.apache.cxf.message.Message.ENDPOINT_ADDRESS=APPLICATION}, org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://localhost:5555/studentws?wsdl, java.lang.reflect.Method=public abstract boolean com.webservice.StudentWsImpl.addStudentService(java.lang.String,java.lang.String,java.lang.String)}}, org.apache.cxf.client=true, client.holders=[null, null, null], org.apache.cxf.jaxws.context.WrappedMessageContext.SCOPES={org.apache.cxf.message.Message.ENDPOINT_ADDRESS=APPLICATION}, org.apache.cxf.mime.headers={}, org.apache.cxf.message.inbound=false, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.ws.policy.EffectivePolicy=org.apache.cxf.ws.policy.EffectivePolicyImpl@481ba2cf, java.lang.reflect.Method=public abstract boolean com.webservice.StudentWsImpl.addStudentService(java.lang.String,java.lang.String,java.lang.String), http.scheme=http, Content-Type=text/xml}
null#null
null#null
-------out Interceptor Client-------
-------into Interceptor Client-------
{org.apache.cxf.message.Message.PROTOCOL_HEADERS={Content-Length=[241], content-type=[text/xml; charset=UTF-8], Date=[Tue, 17 Apr 2018 16:39:14 GMT], Server=[Jetty(9.2.22.v20170606)]}, org.apache.cxf.ws.policy.AssertionInfoMap={}, org.apache.cxf.transport.Conduit=conduit: class org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit57241990target: http://localhost:5555/studentws?wsdl, org.apache.cxf.client=true, org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@1583741e, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.message.inbound=true, org.apache.cxf.message.Message.RESPONSE_CODE=200, Content-Type=text/xml; charset=UTF-8}
null#null
null#null
-------out Interceptor Client-------
-------into Interceptor Client-------
{org.apache.cxf.message.Message.PROTOCOL_HEADERS={Accept=[*/*], SOAPAction=[""]}, org.apache.cxf.transport.Conduit=conduit: class org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit57241990target: http://localhost:5555/studentws?wsdl, use.async.http.conduit=false, org.apache.cxf.service.model.MessageInfo=[MessageInfo INPUT: {http://serviceImpl.service.myl.com/}addStudentService], org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@1583741e, org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://localhost:5555/studentws?wsdl, org.apache.cxf.headers.Header.list=[], schema-validation-enabled=NONE, org.apache.cxf.request.method=POST, http.connection.address=org.apache.cxf.transport.http.Address@5b367418, wrote.envelope.start=true, http.connection=sun.net.www.protocol.http.HttpURLConnection:http://localhost:5555/studentws?wsdl, org.apache.cxf.service.model.BindingMessageInfo=org.apache.cxf.service.model.BindingMessageInfo@36060e, org.apache.cxf.invocation.context={ResponseContext={}, RequestContext={org.apache.cxf.jaxws.context.WrappedMessageContext.SCOPES={org.apache.cxf.message.Message.ENDPOINT_ADDRESS=APPLICATION}, org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://localhost:5555/studentws?wsdl, java.lang.reflect.Method=public abstract boolean com.webservice.StudentWsImpl.addStudentService(java.lang.String,java.lang.String,java.lang.String)}}, org.apache.cxf.client=true, client.holders=[null, null, null], org.apache.cxf.jaxws.context.WrappedMessageContext.SCOPES={org.apache.cxf.message.Message.ENDPOINT_ADDRESS=APPLICATION}, org.apache.cxf.mime.headers={}, org.apache.cxf.message.inbound=false, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.ws.policy.EffectivePolicy=org.apache.cxf.ws.policy.EffectivePolicyImpl@481ba2cf, java.lang.reflect.Method=public abstract boolean com.webservice.StudentWsImpl.addStudentService(java.lang.String,java.lang.String,java.lang.String), http.scheme=http, Content-Type=text/xml}
null#null
null#null
-------out Interceptor Client-------
-------into Interceptor Client-------
{org.apache.cxf.message.Message.PROTOCOL_HEADERS={Content-Length=[241], content-type=[text/xml; charset=UTF-8], Date=[Tue, 17 Apr 2018 16:39:14 GMT], Server=[Jetty(9.2.22.v20170606)]}, org.apache.cxf.ws.policy.AssertionInfoMap={}, org.apache.cxf.transport.Conduit=conduit: class org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit57241990target: http://localhost:5555/studentws?wsdl, org.apache.cxf.client=true, org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@1583741e, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.message.inbound=true, org.apache.cxf.message.Message.RESPONSE_CODE=200, Content-Type=text/xml; charset=UTF-8}
null#null
null#null
-------out Interceptor Client-------

简单测试,如有错误请多指点

WebService学习总结(五)--CXF的拦截器的更多相关文章

  1. 【WebService】WebService之CXF的拦截器(五)

    CXF拦截器介绍 CXF拦截器是功能的主要实现单元,也是主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加功能.当服务被调用时,会经过多个拦截器链(Interceptor Chain)处理,拦 ...

  2. CXF之五 拦截器Interceptor

    拦截器(Interceptor)是CXF功能最主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加很多功能.拦截器和JAX-WS Handler.Filter的功能类似,当服务被调用时,就会创建 ...

  3. 【CXF】- 拦截器 Interceptor

    CXF拦截器 拦截动态操作请求和响应数据 拦截器分类 位置:服务器端拦截器,客户端拦截器 消息方向:入拦截器 出拦截器 定义者:系统拦截器 自定义拦截器:LoggingInInteceptor ①:创 ...

  4. CXF添加拦截器和自定义拦截器

    前面讲了如何采用CXF开发webservice,现在来讲如何添加拦截器和自定义拦截器. 服务端代码: HelloWorld implementor=new HelloWorldImpl(); Stri ...

  5. Apache CXF自定义拦截器

    为什么设计拦截器?1.为了在webservice请求过程中,能动态操作请求和响应数据,CXF设计了拦截器 拦截器分类: 1.按所处的位置分:服务器端拦截器,客户端拦截器. 2.按消息的方向分:入拦截器 ...

  6. CXF 自定义拦截器

    此例子来自apache cxf sample. /**  * Licensed to the Apache Software Foundation (ASF) under one  * or more ...

  7. (八)CXF添加自定义拦截器

    前面我们说到CXF添加内置的拦截器,今天的话,我们来讲下如何添加自定义拦截器: 我们的实例是客户端访问服务端webservice接口要加权限认证. 我们思路先说下.我们可以通过在SOAP消息的Head ...

  8. (七)CXF添加拦截器

    今天开始讲下拦截器,前面大家学过servlet,struts2 都有拦截器概念,主要作用是做一些权限过滤,编码处理等: webservice也可以加上拦截器,我们可以给webservice请求加权限判 ...

  9. Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7098753.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十六)--S ...

随机推荐

  1. MySQL中特别实用的几种SQL语句【转】

    一.插入或替换 如果我们想插入一条新记录(INSERT),但如果记录已经存在,就先删除原记录,再插入新记录. 情景示例:这张表存的每个客户最近一次交易订单信息,要求保证单个用户数据不重复录入,且执行效 ...

  2. 记录21.07.24 —— Vue的组件与路由

    VUE组件 作用:复用性 创建组件的三种方式 第一种:使用extends搭配component方法 第二种:直接使用component方法 只有用vue声明且命名的才称之为创建组件 注意:templa ...

  3. LVM磁盘配额

    目录 一.LVM概述 1.1.逻辑卷管理 1.2.LVM机制的基本概念 二.LVM管理命令 三.磁盘配额概述 3.1.实现磁盘配额的条件 3.2.Linux磁盘限额的特点 3.3.常用命令及选项 3. ...

  4. 干了5年Android开发,突然感觉自己啥也不会,啥也不想干,还要继续吗?

    这是在某论坛看到的一名同行的吐槽: 我干了差不多5年,不过给人感觉跟只有两三年的人一样. 我觉得我不适合干程序员,主要是新东西的接受能力比其他人慢,Android技术又更新得很快,感觉总是跟不上.年纪 ...

  5. Kotlin高阶函数实战

    前言 1. 高阶函数有多重要? 高阶函数,在 Kotlin 里有着举足轻重的地位.它是 Kotlin 函数式编程的基石,它是各种框架的关键元素,比如:协程,Jetpack Compose,Gradle ...

  6. Nacos 笔记

    Nacos 笔记 目录 Nacos 笔记 1. Nacos简介 1.1 主流配置中心对比 1.2 主流注册中心对比 1.3 Nacos特性 2. 安装启动 支持外部 MySQL 3. 配置管理 3.1 ...

  7. 深入理解jvm-2Edition-虚拟机类加载机制

    1.概述-什么是类加载? 将Class文件从其他地方(外存.字节流甚至是网络流中)载入内存, 并对其中数据进行校验.转换解析和初始化,最终从其中提取出能够被虚拟机使用的Java类型. 用图纸造模子,该 ...

  8. 计算机网络 中国大学MOOC 哈尔滨工业大学 习题答案

    转自 https://blog.csdn.net/qq_37514135/article/details/82733651 计算机网络作业题 第一章 第一题 如图所示网络.A在t=0时刻开始向C发送一 ...

  9. 【笔记】使用scikit-learn解决回归问题

    使用sklearn解决回归问题 依然是加载数据 import numpy as np import matplotlib.pyplot as plt from sklearn import datas ...

  10. 【XSS-labs】Level 11-15

    Level 11 和level 10 差不多的页面,传参后查看页面源代码:依旧是第3个可以正常传参. 尝试level 10 的payload 发现 " 被实体化 可以打开控制台将第三个inp ...