CXF之日志拦截器的使用

0.现在我们有了一个服务端和一个客户端,也能够正常调用了。不过为了能够快速的定位BUG,我们需要知道每一次发送和接收的消息内容。CXF自带的日志拦截器可以实现这个功能。这次我们来试着配置它。


1.什么是拦截器?

当客户端向服务端发送请求,请求中的数据将被组装并传输到服务器。服务器获取该数据,解组,最后调用服务方法。当服务器发送响应给客户端时,将重复该过程。组装和解组是客户端和服务端提供的核心功能。CXF通过Interceptor来提供这些功能。
     Interceptor通过监听传输过来的信息来提供核心功能。这些功能包括:组装、解组、操纵消息头、执行认证检查、验证消息数据等。CXF提供内置的Interceptor来实现这些功能。用户也可以自定义Interceptor。Interceptor以phases组织起来,以链的形式调用, 

2.interceptor是如何工作的?

当接收消息或发送消息时,框架会将xml或soap对象传给PhaseInterceptorChain。PhaseInterceptorChain是一个责任链,其中的每一个环节是一个interceptor。当整个PhaseInterceptorChain里的所有interceptor执行完之后,就完成了xml到对象或对象到xml的转变。CXF将chain分成了inbound和outbound两个,分别处理接受消息和发送消息。chain里的interceptor的顺序由phase来确定,每一个interecptor在初始化的时候,都会指定一个phase。而每一个phase有一个int型的顺序值,在初始化chain的时候,会按照每个interceptor的phase来确定前后顺序。


3.什么是日志拦截器?

日志拦截器是CXF自带的两个拦截器,分别对应了inbound链和outbound链。


4.配置服务端日志拦截器

该配置是基于之前的CXF之Simple服务器搭建进行修改

<?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:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd">
	
	<jaxws:endpoint id="helloWorld" implementor="com.tangjun.example.cxf.service.impl.HelloWorldImpl" address="/helloWorld">
		<jaxws:inInterceptors>
			<bean name="logInInter" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<bean name="logOutInter" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
		</jaxws:outInterceptors>
	</jaxws:endpoint>
</beans>

5.使用soapUI调用服务验证结果

信息: Inbound Message
----------------------------
ID: 1
Address: http://localhost:9090/exmaple_cxf_server/services//helloWorld
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml;charset=UTF-8
Headers: {accept-encoding=[gzip,deflate], connection=[Keep-Alive], Content-Length=[241], content-type=[text/xml;charset=UTF-8], host=[localhost:9090], SOAPAction=[""], user-agent=[Apache-HttpClient/4.1.1 (java 1.5)]}
Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:int="http://interfaces.service.cxf.example.tangjun.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <int:sayHi/>
   </soapenv:Body>
</soapenv:Envelope>
--------------------------------------


信息: Outbound Message
---------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHiResponse xmlns:ns2="http://interfaces.service.cxf.example.tangjun.com/"><return>helloworld, Wed Apr 22 17:35:03 CST 2015</return></ns2:sayHiResponse></soap:Body></soap:Envelope>
--------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值