自定义客户端输出拦截器:
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; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。