自定义客户端输出拦截器: package cxf.client; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.Phase; public class MyClientInterceptor extends AbstractSoapInterceptor { public MyClientInterceptor() { //一定要指定这个拦截器放在拦截器链的哪个阶段,这里放在最开始阶段。 super(Phase.SETUP); } @Override public void handleMessage(SoapMessage message) throws Fault { System.out.println("client: MyClientInterceptor!!!!"); } } 客户端调用配置: <?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:client id="client" address="http://localhost:8085/java_first_spring_support1/service/HelloWorld" serviceClass="cxf.server.HelloWorld"> <jaxws:outInterceptors> <bean class="cxf.client.MyClientInterceptor"/> </jaxws:outInterceptors> </jaxws:client> </beans> 客户端调用: package cxf.client; import org.springframework.context.support.ClassPathXmlApplicationContext; import cxf.server.HelloWorld; public final class Client { public static void main(String args[]) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"}); HelloWorld client = (HelloWorld)context.getBean("client"); String response = client.sayHi("Joe"); System.out.println("Response: " + response); System.exit(0); } } 自定义服务端输出拦截器: package cxf.server; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.Phase; public class MyServerInterceptor extends AbstractSoapInterceptor { public MyServerInterceptor() { //一定要指定这个拦截器放在拦截器链的哪个阶段,这里放在最开始阶段。 super(Phase.RECEIVE ); } @Override public void handleMessage(SoapMessage message) throws Fault { System.out.println("server: MyServerInterceptor!!!"); } } 服务端配置: <?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:endpoint id="helloWorld" implementor="cxf.server.HelloWorldImpl" address="/HelloWorld"> <jaxws:inInterceptors> <bean class="cxf.server.MyServerInterceptor"/> </jaxws:inInterceptors> </jaxws:endpoint> </beans> 服务接口: package cxf.server; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(String text); } 服务接口实现: package cxf.server; import javax.jws.WebService; @WebService(endpointInterface = "cxf.server.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { System.out.println("sayHi called"); return "Hello " + text; } }