`
hougbin
  • 浏览: 502385 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

CXF bus interceptor配置

    博客分类:
  • SOA
阅读更多

作用:BUS是cxf的支架,它主要担当扩展及拦截器提供者的角色。

 

在这里主要讲讲 bus的interceptor的功能

目前配置cxf的interceptor主要有2中方法:

1、通过xml配置文件的方法,使用<cxf:bus>
2、通过在java代码中使用编码的方式来添加拦截器

 

下面来看2个例子

1 配置文件方式配置 cxf bus interceptor

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="logOutbound" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>

    <cxf:bus>
        <cxf:outInterceptors>
            <ref bean="logOutbound"/>
        </cxf:outInterceptors>
    </cxf:bus>
</beans>
注意:在使用<cxf:bus>时候,一定要引入 命名空间xmlns:cxf=http://cxf.apache.org/core,及其对应的模式http://cxf.apache.org/schemas/core.xsd
2、java代码硬编码方式添加拦截器

 在服务端添加拦截

import javax.xml.ws.Endpoint;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;

Object implementor = new GreeterImpl();
EndpointImpl ep = (EndpointImpl) Endpoint.publish("http://localhost/service", implementor);

ep.getServiceFactory().getBus().getInInterceptors().add(new LoggingInInterceptor());
ep.getServiceFactory().getBus().getOutInterceptors().add(new LoggingOutInterceptor());

 

在客户端添加拦截

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

public class WSClient {
    public static void main (String[] args) {
        MyService ws = new MyService();
        MyPortType port = ws.getPort();
       
        Client client = ClientProxy.getClient(port);
        client.getInInterceptors().add(new LoggingInInterceptor());
        client.getOutInterceptors().add(new LoggingOutInterceptor());

另外在java代码中也可以通过使用注解的方式添加拦截器

在SEI及SEI的实现类中添加注解有着不同的含义,若在SEI中添加拦截器注解,则表示其会在client及server端都起作用,若在SEI的实现类中添加注解,则只会在server端起作用,例子:

SEI 实现类

@javax.jws.WebService(portName = "MyWebServicePort", serviceName = "MyWebService", ...)
@Features(features = "org.apache.cxf.feature.LoggingFeature")       
public class MyWebServicePortTypeImpl implements MyWebServicePortType {

 

效果等同于如下代码

import org.apache.cxf.interceptor.InInterceptors;
import org.apache.cxf.interceptor.OutInterceptors;

@javax.jws.WebService(portName = "WebServicePort", serviceName = "WebServiceService", ...)
@InInterceptors(interceptors = "org.apache.cxf.interceptor.LoggingInInterceptor")
@OutInterceptors(interceptors = "org.apache.cxf.interceptor.LoggingOutInterceptor")

public class WebServicePortTypeImpl implements WebServicePortType {

1
0
分享到:
评论
3 楼 wangyudong 2017-11-27  
由CXF实现的微服务需要有比较好的工具去测试RESTful API,很多REST Client是不支持自动化测试RESTful API,也不支持自动生成API文档.
之前习惯用一款名字为 WisdomTool REST Client,支持自动化测试RESTful API,输出精美的测试报告,并且自动生成精美的RESTful API文档。
轻量级的工具,功能却很精悍哦!

https://github.com/wisdomtool/rest-client

Most of REST Client tools do not support automated testing.

Once used a tool called WisdomTool REST Client supports automated testing, output exquisite report, and automatically generating RESTful API document.

Lightweight tool with very powerful features!

https://github.com/wisdomtool/rest-client
2 楼 jackygrape 2010-10-18  
hfkiss44 写道
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />


	<!-- CXF IP地址输入拦截器 -->  
	<bean id="ipAddressInInterceptor"  
	    class="demo.spring.cxffilter.CXFIpAddressInInterceptor" />
	    <!-- 全局Bus(输入拦截器) -->  
	<cxf:bus>  
	    <cxf:inInterceptors>  
	        <ref bean="ipAddressInInterceptor" />    
	    </cxf:inInterceptors>  
	</cxf:bus>  
	     
    
	<jaxws:endpoint 
	  id="helloWorld" 
	  implementor="demo.spring.HelloWorldImpl" 
	  address="/HelloWorld" />
	  
</beans>
把bus的那个加上 后  jaxws标签就认不出来了

不会的
1 楼 hfkiss44 2010-08-25  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />


	<!-- CXF IP地址输入拦截器 -->  
	<bean id="ipAddressInInterceptor"  
	    class="demo.spring.cxffilter.CXFIpAddressInInterceptor" />
	    <!-- 全局Bus(输入拦截器) -->  
	<cxf:bus>  
	    <cxf:inInterceptors>  
	        <ref bean="ipAddressInInterceptor" />    
	    </cxf:inInterceptors>  
	</cxf:bus>  
	     
    
	<jaxws:endpoint 
	  id="helloWorld" 
	  implementor="demo.spring.HelloWorldImpl" 
	  address="/HelloWorld" />
	  
</beans>
把bus的那个加上 后  jaxws标签就认不出来了

相关推荐

    spring-cxf 日志记录配置.zip

    你可以配置`&lt;cxf:bus&gt;`元素下的`&lt;cxf:features&gt;`或`&lt;cxf:properties&gt;`标签来控制日志级别和实现。 - 例如,添加`&lt;cxf:property key="org.apache.cxf.logging.FrontendLoggerClass" value="org.apache.cxf.common....

    CXF使用EndpointImpl发布WebService加入拦截器

    首先,我们要理解`EndpointImpl`是CXF框架用于构建和配置Web服务端点的核心类。通过实例化这个类,我们可以自定义服务的行为,包括添加拦截器,以实现诸如日志记录、安全验证、性能统计等额外功能。 拦截器在CXF中...

    13.为CXF与Spring整合发布WebService添加拦截器进行权限控制

    首先,我们需要创建一个实现了`org.apache.cxf.interceptor.Fault`和`org.apache.cxf.phase.PhaseInterceptor`接口的类。这个类通常会包含处理权限检查的业务逻辑。例如,你可以定义一个名为`...

    CXF和Spring整合,并且添加拦截器

    2. **配置Spring**:在Spring配置文件中,我们需要声明CXF的服务端点(Endpoint)和相关的Bus实例。Spring会自动管理这些组件,提供服务的生命周期控制。 ```xml xmlns:xsi=...

    CXF3.0+Spring3.2 自定义拦截器

    3. **Spring集成**:在Spring3.2中,我们可以使用`&lt;cxf:bus&gt;`和`&lt;cxf:interceptor&gt;`标签将自定义拦截器注册到CXF Bus中。这样,Spring容器会管理拦截器的生命周期,并在需要时注入其他依赖。 4. **拦截器链**:CXF...

    基于CXF的webservice的发布及访问

    &lt;cxf:bus-ref&gt;myBus&lt;/cxf:bus-ref&gt; &lt;/cxf:client&gt; ``` 4. **执行调用**:在Spring配置生效后,可以直接通过客户端接口进行服务调用。 **总结** Apache CXF提供了简洁的API和工具,使得发布和调用SOAP 1.2 Web...

    CXF 添加soap 头部信息.zip_CXF增加soap头验证_bluex8z_cxf_meltedkw7_soap信息头

    将自定义的Interceptor添加到CXF服务端或客户端的配置中。如果是Spring配置,可以在`cxf.xml`文件中添加以下代码: ```xml &lt;cxf:bus&gt; &lt;cxf:outInterceptors&gt; &lt;/cxf:outInterceptors&gt; &lt;/cxf:bus&gt; ``` 4. *...

    apache cxf 教程

    Bus 是整个 CXF 架构的核心,类似于 Spring 中的 ApplicationContext,用于管理和配置 CXF 的各种资源,例如 WSDL 管理器、绑定工厂等。Bus 提供了一个可扩展的环境,允许开发者自定义资源或替换默认资源。默认情况...

    08.CXF拦截器的理论以及如何为CXF的客户端和服务器端添加拦截器

    - `org.apache.cxf.interceptor.ClientInterceptor` 和 `org.apache.cxf.interceptor.ServerInterceptor`:分别用于客户端和服务端。 - `org.apache.cxf.phase.PhaseInterceptor`:通用接口,适用于任何拦截器。 ...

    CXF WebService 开发指南、技术文档

    - 使用`Bus`或`Endpoint`的配置选项。 例如,在客户端添加一个Interceptor来记录请求和响应信息: ```java // 定义Interceptor public class LoggingInterceptor extends AbstractPhaseInterceptor&lt;Message&gt; { ...

    CXF_WebService_开发指南、技术文档

    #### 二、CXF环境搭建与配置 CXF的安装非常简单,只需要下载官方提供的jar包并将其加入到项目中即可。CXF官方网站为[http://cxf.apache.org/](http://cxf.apache.org/),其中提供了详细的文档和支持。 **下载地址*...

    WebService发布框架CXF的源码

    - `org.apache.cxf.interceptor.Interceptor`:处理消息的拦截器,提供了对请求和响应进行预处理和后处理的机制。 - `org.apache.cxf.frontend.ServerFactoryBean`和`ClientFactoryBean`:分别用于配置和创建服务器...

    CXF服务端和客户端 应用开发指南

    此外,还需配置CXF的Bus实例,这是CXF框架的核心组件,负责处理服务请求的路由和转换。 #### 五、服务发布与客户端调用 服务发布后,客户端可通过调用特定的URL访问服务。CXF支持多种客户端编程模型,包括JAX-WS和...

    CXF 入门示例

    BusFactory负责创建Bus,SpringBusFactory是默认实现,它会自动加载META-INF/cxf目录下的Spring配置文件,以构建ApplicationContext。开发者可以自定义配置文件以定制Bus的行为。 **2. 消息传递和拦截器** CXF基于...

    CXF Web Server 教程

    BusFactory负责创建Bus,SpringBusFactory是默认实现,它会读取META-INF/cxf目录下的配置文件来构建ApplicationContext。 2. **消息传递和拦截器**:CXF基于一个通用的消息层,由消息、拦截器和拦截器链构成。拦截...

Global site tag (gtag.js) - Google Analytics