CXF拦截器

引言

CXF拦截器是Apache CXF里面一个很重要的功能,它能够动态操作响应数据和请求数据,降低代码的耦合性,提供代码的内聚性。这对于CXF这个以处理消息为中心的服务框架来说是非常有用的,CXF通过在Interceptor中对消息进行特殊处理,实现了很多重要功能模块,例如:日志记录,Soap消息处理,消息的压缩处理。

一、拦截器分类

1.按所处的位置分:服务器端拦截器、客户端拦截器

2.按消息的方向分:入拦截器、出拦截器

3.按定义者分:系统拦截器,自定义拦截器

二、拦截器API

在最顶层有个拦截器接口 Interceptor接口

AbstractPhaseInterceptor(自定义拦截器继承这个接口)

    LoggingInterceptor(系统日志入拦截器类)

    LoggingOutInterceptor(系统日志出拦截器类)


三、添加日志入拦截器

继续用上篇的CXF WebService代码,再添加一个日志入拦截器。

package com.gpj.service.endpoint;

import java.util.List;

import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws22.EndpointImpl;
import org.apache.cxf.message.Message;

import com.gpj.service.HelloServiceImpl;

public class ServiceTest {

	public static void main(String[] args) {
		String address="http://localhost:8888/service/HelloService";
		Endpoint publish = Endpoint.publish(address, new HelloServiceImpl());
		EndpointImpl endpointImpl = (EndpointImpl) publish;
		
		//服务端的日志入拦截器
		List<Interceptor<? extends Message>> inFaultInterceptors = endpointImpl.getInFaultInterceptors();
		inFaultInterceptors.add(new LoggingInInterceptor());
		
		//服务器端日志出拦截器
		List<Interceptor<? extends Message>> outInterceptors = endpointImpl.getOutInterceptors();
		outInterceptors.add(new LoggingInInterceptor());
		
		System.out.println("JDKservice发布成功");
	}
}

再次运行main方法。打开eclipse的Web Service Explorer,访问http://localhost:8080/service/HelloService?wsdl


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值