`

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

 
阅读更多

上一篇文章已经写了spring和cxf的整合,这篇文章主要写怎么添加cxf的拦截器(Intercepter)

 

通过Intercepter可以灵活的设置cxf客户端和服务器端代码而不用修改主业务代码

 

下面写了一个简单的例子,也可以通过该例子改造成权限验证等

 

代码如下:

SampleInterceptor

写道
package com.benben.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;

@SuppressWarnings("rawtypes")
public class SampleInterceptor extends AbstractPhaseInterceptor {

public SampleInterceptor() {
super(Phase.PRE_INVOKE);

}



public void handleMessage(Message message) throws Fault {
System.out.println("############handleMessage##########");
System.out.println(message);
if (message.getDestination() != null) {
System.out.println(message.getId() + "#" + message.getDestination().getMessageObserver());
}
if (message.getExchange() != null) {
System.out.println(message.getExchange().getInMessage() + "#" + message.getExchange().getInFaultMessage());
System.out.println(message.getExchange().getOutMessage() + "#" + message.getExchange().getOutFaultMessage());
}
}

}

 

服务器端主要配置文件为:

写道
<?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">
<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"/>


<bean id="userServiceBean" class="com.benben.webservice.service.impl.UserServiceImpl"/>

<bean id="inMessageInterceptor" class="com.benben.Interceptor.SampleInterceptor">
<constructor-arg value="receive"/>
</bean>

<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
<jaxws:server id="userService" serviceClass="com.benben.webservice.service.UserService" address="/Users">
<jaxws:serviceBean>
<!-- 要暴露的 bean 的引用 -->
<ref bean="userServiceBean"/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<ref bean="inMessageInterceptor"/> 
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outLoggingInterceptor"/>
</jaxws:outInterceptors>
</jaxws:server>



</beans>

 客户端主要配置文件

写道
<?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">

<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"/>

<jaxws:client id="userWsClient" serviceClass="com.benben.webservice.service.UserService"
address="http://localhost/cxfTest/services/Users">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean class="com.benben.Interceptor.SampleInterceptor"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="com.benben.Interceptor.SampleInterceptor"/>
<!--<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>-->
</jaxws:outInterceptors>

</jaxws:client>
</beans>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics