cxf 拦截器入门

1、定义一个对外提供的接口

package interceptor;

import javax.jws.WebService;

@WebService
public interface IInterceptorSendMsg {
	
	public Boolean sendMsg();
	
	public String receiveMsg();
	
}

 

2、实现定义接口的实现类

package interceptor;

import java.util.Date;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService(endpointInterface="interceptor.IInterceptorSendMsg")
@SOAPBinding(style = Style.RPC)
public class InterceptorSendMsg implements IInterceptorSendMsg{

	@Override
	public Boolean sendMsg() {
		System.out.println("sendMsg");
		return true;
	}

	@Override
	public String receiveMsg() {
		System.out.println("receiveMsg");
		return new Date().toString();
	}

}

 

3、自己定义一个输入拦截器

package interceptor;

import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class MyInteceptor extends AbstractPhaseInterceptor<Message> {

	private Map<String, String> paramMap = new HashMap<String, String>();
	private String sign;

	public MyInteceptor(String phase) {
		super(Phase.RECEIVE);
		paramMap.put("1", "robert");
		paramMap.put("2", "green");
		paramMap.put("3", "john");
		paramMap.put("4", "smith");
		this.sign = "2";
	}

	@Override
	public void handleMessage(Message msg) throws Fault {
		System.out.println("MyInteceptor: do match person ");
		System.out.println(paramMap.get(sign));
		System.out.println("msg.getId() ---- " + msg.getId());
	}

}

 

备注:

1、自定义拦截器需要继承 AbstractPhaseInterceptor 类。

2、自定义类需要有一个构造方法,主要是为了说明这个拦截器在什么时机做动作(receive,pre_stream等)。org.apache.cxf.phase.Phase 类的静态属性说明。

3、实现handleMessage方法。

 

4、自定定义一个输出拦截器

package interceptor;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class MyInteceptorOuter extends AbstractPhaseInterceptor<Message>{

	public MyInteceptorOuter(String phase) {
		super(Phase.PRE_STREAM);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void handleMessage(Message msg) throws Fault {
		// TODO Auto-generated method stub
		System.out.println("MyInteceptorOuter");
		System.out.println(msg.getContextualProperty("result"));
	}

}

 

5、启动服务器

package server;

import interceptor.IInterceptorSendMsg;
import interceptor.InterceptorSendMsg;
import interceptor.MyInteceptor;
import interceptor.MyInteceptorOuter;
import hb.ISessionSendMsg;
import hb.SessionSendMsg;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class InterceptorServer {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JaxWsServerFactoryBean factoryBean1 = new JaxWsServerFactoryBean();
		InterceptorSendMsg interceptor = new InterceptorSendMsg();
		factoryBean1.setAddress("http://localhost:8080/wscxf/interceptor");
		factoryBean1.setServiceClass(IInterceptorSendMsg.class);//接口类
		factoryBean1.setServiceBean(interceptor);
		//输入记录日志
		factoryBean1.getInInterceptors().add(new LoggingInInterceptor());  
		//输出记录日志
		factoryBean1.getOutInterceptors().add(new LoggingOutInterceptor());  
		//接收数据拦截器
		factoryBean1.getInInterceptors().add(new MyInteceptor("2"));  
		//发送数据拦截器
		factoryBean1.getInInterceptors().add(new MyInteceptorOuter("sdaf"));
		
		factoryBean1.create();
	}

}

 

备注:

1、 JaxWsServerFactoryBean 对象用来添加需要的拦截器

2、LoggingInInterceptor 和 LoggingOutInterceptor 拦截器是CXF自带的拦截器,在开发的时候可以放开,用来调试

 

6、测试

package client;
import interceptor.IInterceptorSendMsg;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class InterceptorClient {

	public static void main(String[] args) {
		//调用WebService
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(IInterceptorSendMsg.class);
        factory.setAddress("http://localhost:8080/wscxf/interceptor");
        
        IInterceptorSendMsg interceptorSendMsg = (IInterceptorSendMsg)factory.create();
        
        System.out.println(interceptorSendMsg.sendMsg());
        
        System.out.println(interceptorSendMsg.receiveMsg());
	}

}

 

备注:

客户端调用使用的类是JaxWsProxyFactoryBean,服务器启动调用的类是 JaxWsServerFactoryBean

 

你可能感兴趣的:(CXF)