CXF与Spring集成
jopen
11年前
服务端是Web Project,客户端是Java Project,先看服务端代码
首先是SEI,即服务端接口类HelloService.java
package com.jadyer.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService(targetNamespace="http://blog.csdn.net/jadyer") public interface HelloService { @WebMethod @WebResult(name="sayHelloResult") public String sayHello(@WebParam(name="name")String name); }
然后是SIB,即服务端接口实现类HelloServiceImpl.java
package com.jadyer.service; import javax.jws.WebService; import org.springframework.stereotype.Service; @WebService(endpointInterface="com.jadyer.service.HelloService", targetNamespace="http://blog.csdn.net/jadyer") @Service public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { System.out.println("Receive the name=[" + name + "]"); if(null==name){ return "Hello,World"; }else{ return "Hello," + name; } } }
下面是服务端自定义的拦截器类LicenseInInterceptor.java
package com.jadyer.interceptor; import java.util.List; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; import org.apache.cxf.headers.Header; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.Phase; import org.springframework.stereotype.Component; import org.w3c.dom.Node; @Component public class LicenseInInterceptor extends AbstractSoapInterceptor { public LicenseInInterceptor(){ super(Phase.INVOKE); } @Override public void handleMessage(SoapMessage message) throws Fault { List<Header> headers = message.getHeaders(); Object obj = null; for(Header header : headers){ if(header.getName().getLocalPart().equals("licenseInfo")){ obj = header.getObject(); if(obj instanceof Node){ System.out.println("Receive the licenseInfo=[" + ((Node)obj).getTextContent() + "]"); } } } } }
下面是服务端的Spring配置文件applicationContext.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <context:component-scan base-package="com.jadyer"/> <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"/> <!-- 发布WebServices服务 --> <!-- 也可以写成这样<jaxws:endpoint implementor="com.jadyer.service.HelloServiceImpl" address="/myHello"/> --> <!-- 最后的访问地址都是http://127.0.0.1:8088/webPath/services/myHello?wsdl --> <!-- 经测试,写成address="myHello"的话,也是可以的,访问地址和上一行贴出来的是一样的 --> <jaxws:endpoint implementor="#helloServiceImpl" address="/myHello"> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <ref bean="licenseInInterceptor"/> </jaxws:inInterceptors> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> </jaxws:outInterceptors> </jaxws:endpoint> <!-- 也可以用CXF提供的core.xsd来注册Interceptor,样例见本文客户端代码 --> <!-- xmlns:cxf="http://cxf.apache.org/core" --> <!-- xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd" --> <!-- 然后像下面这样写就可以了 <jaxws:endpoint implementor="#helloServiceImpl" address="/myHello"/> <cxf:bus> <cxf:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <ref bean="licenseInInterceptor"/> </cxf:inInterceptors> <cxf:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> </cxf:outInterceptors> </cxf:bus> --> </beans>
这个是服务端用来方便查看Spring日志的log4j.properties
#http://blog.csdn.net/jadyer/article/details/7833395 log4j.rootLogger=DEBUG,CONSOLE #通常用于框架日志,如mina,spring等 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=DEBUG log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}]%m%n
最后是服务端的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
OK,服务端代码示例完毕,下面是客户端代码</span>
首先是客户端用于发送SOAPHeader信息的拦截器类LicenseOutInterceptor.java
package com.jadyer.interceptor; import javax.xml.bind.JAXBException; import javax.xml.namespace.QName; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; import org.apache.cxf.databinding.DataBinding; import org.apache.cxf.headers.Header; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.jaxb.JAXBDataBinding; import org.apache.cxf.phase.Phase; import org.springframework.stereotype.Component; @Component public class LicenseOutInterceptor extends AbstractSoapInterceptor{ public LicenseOutInterceptor(){ super(Phase.WRITE); } @Override public void handleMessage(SoapMessage message) throws Fault { QName qname = new QName("http://blog.csdn.net/jadyer", "licenseInfo", "ns"); DataBinding dataBinding = null; try { dataBinding = new JAXBDataBinding(String.class); } catch (JAXBException e) { e.printStackTrace(); } Header header = new Header(qname, "Jadyer", dataBinding); message.getHeaders().add(header); } }
下面是客户端模拟的一个Service实现类ClientService.java
package com.jadyer.service; import javax.annotation.Resource; import net.csdn.blog.jadyer.HelloService; import org.springframework.stereotype.Service; @Service public class ClientService { @Resource private HelloService myServerWebService; public String getServerResponse(String name){ return myServerWebService.sayHello(name); } }
下面是客户端的Spring配置文件applicationContext.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <context:component-scan base-package="com.jadyer"/> <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="myServerWebService" serviceClass="net.csdn.blog.jadyer.HelloService" address="http://127.0.0.1:8088/cxf_04_spring/services/myHello"/> <cxf:bus> <cxf:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> </cxf:inInterceptors> <cxf:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> <ref bean="licenseOutInterceptor"/> </cxf:outInterceptors> </cxf:bus> </beans>
最后是客户端调用服务端的模拟入口ClientApp.java
注意:具体的客户端WebService代码是由wsdl2java生成的
package com.jadyer.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jadyer.service.ClientService; public class ClientApp { public static void main(String[] args) { //wsdl2java -d D:/Download/ -frontend jaxws21 -keep -verbose http://127.0.0.1:8088/cxf_04_spring/services/myHello?wsdl //wsdl2java命令与wsimport相似,wsimport简介详见http://blog.csdn.net/jadyer/article/details/8692108 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); ClientService cs = (ClientService)ctx.getBean("clientService"); System.out.println(cs.getServerResponse("玄玉")); } }
老习惯,把控制台输出也贴出来</span>
这是服务端的控制台输出
2013-5-31 23:26:08 org.apache.cxf.services.HelloServiceImplService.HelloServiceImplPort.HelloService 信息: Inbound Message ---------------------------- ID: 1 Address: http://127.0.0.1:8088/cxf_04_spring/services/myHello Encoding: UTF-8 Http-Method: POST Content-Type: text/xml; charset=UTF-8 Headers: {Accept=[*/*], cache-control=[no-cache], connection=[keep-alive], Content-Length=[302], content-type=[text/xml; charset=UTF-8], host=[127.0.0.1:8088], pragma=[no-cache], SOAPAction=[""], user-agent=[Apache CXF 2.7.0]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><ns:licenseInfo xmlns:ns="http://blog.csdn.net/jadyer">Jadyer</ns:licenseInfo></soap:Header><soap:Body><ns2:sayHello xmlns:ns2="http://blog.csdn.net/jadyer"><name>玄玉</name></ns2:sayHello></soap:Body></soap:Envelope> -------------------------------------- Receive the name=[玄玉] Receive the licenseInfo=[Jadyer] 2013-5-31 23:26:08 org.apache.cxf.services.HelloServiceImplService.HelloServiceImplPort.HelloService 信息: Outbound Message --------------------------- ID: 1 Encoding: UTF-8 Content-Type: text/xml Headers: {} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://blog.csdn.net/jadyer"><sayHelloResult>Hello,玄玉</sayHelloResult></ns2:sayHelloResponse></soap:Body></soap:Envelope> --------------------------------------
这是客户端的控制台输出
2013-5-31 23:26:08 org.apache.cxf.services.HelloServiceService.HelloServicePort.HelloService 信息: Outbound Message --------------------------- ID: 1 Address: http://127.0.0.1:8088/cxf_04_spring/services/myHello Encoding: UTF-8 Http-Method: POST Content-Type: text/xml Headers: {Accept=[*/*], SOAPAction=[""]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><ns:licenseInfo xmlns:ns="http://blog.csdn.net/jadyer">Jadyer</ns:licenseInfo></soap:Header><soap:Body><ns2:sayHello xmlns:ns2="http://blog.csdn.net/jadyer"><name>玄玉</name></ns2:sayHello></soap:Body></soap:Envelope> -------------------------------------- 2013-5-31 23:26:08 org.apache.cxf.services.HelloServiceService.HelloServicePort.HelloService 信息: Inbound Message ---------------------------- ID: 1 Response-Code: 200 Encoding: UTF-8 Content-Type: text/xml;charset=UTF-8 Headers: {content-type=[text/xml;charset=UTF-8], Date=[Fri, 31 May 2013 15:26:08 GMT], Server=[Apache-Coyote/1.1], transfer-encoding=[chunked]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://blog.csdn.net/jadyer"><sayHelloResult>Hello,玄玉</sayHelloResult></ns2:sayHelloResponse></soap:Body></soap:Envelope> -------------------------------------- Hello,玄玉