`
hougbin
  • 浏览: 490943 次
  • 性别: 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标签就认不出来了

相关推荐

Global site tag (gtag.js) - Google Analytics