Apache CXF实现的SOAP形式的webservices
前言:常用的web service有两种形式,SOAP和RESTful。这里先说SOAP的实现步骤,RESTful的形式后面再说
一 建项目,导jar包
(1)项目结构
(2)在eclipse中新建一个动态web项目“CXFDemo”,并导入cxf中的jar包(路径:/WEB-INF/lib/)。当然,可以去官网下载,也可以直接使用我用过的jar包,链接:http://pan.baidu.com/s/1pKyelAV
(3)配置web.xml:
<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><display-name>CXFDemo</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/service-beans.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></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/*</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list></web-app>
二 web service服务端类
(1)接口CXFService.java:
packagecn.zifangsky;importjavax.jws.WebMethod;importjavax.jws.WebParam;importjavax.jws.WebService;@WebServicepublicinterfaceCXFService{@WebMethodpublicStringsayHello(@WebParam(name="name")Stringname);}
说明:
@WebService:标记表示该接口是一个WebService服务
@WebMethod:标记表示WebService中的方法
@WebParam(name=”paramName”)表示方法中的参数,name属性限制了参数的名称,若没有指定该属性,参数将会被重命名
(2)具体的实现类CXFServiceImpl.java:
packagecn.zifangsky.impl;importcn.zifangsky.CXFService;publicclassCXFServiceImplimplementsCXFService{publicStringsayHello(Stringname){return"Hello,"+name+"\n"+Test.getExtraMessage();}}
注:Test.java类:
packagecn.zifangsky.impl;publicclassTest{publicstaticStringgetExtraMessage(){return"测试";}}
(3)配置service-beans.xml:
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:soap="http://cxf.apache.org/bindings/soap"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"><beanid="outLoggingInterceptor"class="org.apache.cxf.interceptor.LoggingOutInterceptor"/><beanid="loggingFeature"class="org.apache.cxf.feature.LoggingFeature"/><beanid="inLoggingInterceptor"class="org.apache.cxf.interceptor.LoggingInInterceptor"/><jaxws:serverid="sayHelloServices"serviceClass="cn.zifangsky.CXFService"address="/soap/sayHello"><jaxws:serviceBean><beanclass="cn.zifangsky.impl.CXFServiceImpl"/></jaxws:serviceBean><jaxws:outInterceptors><refbean="outLoggingInterceptor"/></jaxws:outInterceptors><jaxws:inInterceptors><refbean="inLoggingInterceptor"/></jaxws:inInterceptors><jaxws:features><refbean="loggingFeature"/><wsa:addressingxmlns:wsa="http://cxf.apache.org/ws/addressing"/></jaxws:features></jaxws:server></beans>
说明:
i)3个bean配置的是日志,在这里把日志相关的配置删掉也不影响web service服务的正常使用
ii)address指的是发布后这个服务的路径问题,比如说我这里就是:http://localhost:8080/CXFDemo/soap/sayHello
(4)测试
发布这个项目到tomcat,运行之后的效果如下:
点击那个链接,可以发现sayHello这个服务的地址是:http://localhost:8080/CXFDemo/soap/sayHello?wsdl
到此,这个web service的服务端已经配置完成
三web service客户端配置
服务端提供服务,而客户端使用服务端提供的服务,因此客户端只需要调用服务端提供的服务就可以了
(1)客户端的项目结构:
(2)同样是新建一个动态web工程,然后引入cxf的jar包,同时需要把服务端的CXFService接口打包成一个jar包,然后导入到客户端的lib中,链接:http://pan.baidu.com/s/1eQVWups
(3)客户端测试类ClientDemo.java:
packagecn.zifangsky.client;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcn.zifangsky.CXFService;publicclassClientDemo{publicstaticvoidmain(Stringargs[]){ClassPathXmlApplicationContextcontext=newClassPathXmlApplicationContext(newString[]{"client-beans.xml"});CXFServiceclientHello=(CXFService)context.getBean("clientHello");Stringresponse=clientHello.sayHello("javaee");context.close();System.out.println("Response:"+response);System.exit(0);}}
(4)客户端配置文件client-beans.xml:
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"><beanid="outLoggingInterceptor"class="org.apache.cxf.interceptor.LoggingOutInterceptor"/><beanid="loggingFeature"class="org.apache.cxf.feature.LoggingFeature"/><beanid="inLoggingInterceptor"class="org.apache.cxf.interceptor.LoggingInInterceptor"/><jaxws:clientid="clientHello"serviceClass="cn.zifangsky.CXFService"address="http://localhost:8080/CXFDemo/soap/sayHello?wsdl"><jaxws:inInterceptors><refbean="inLoggingInterceptor"/></jaxws:inInterceptors><jaxws:outInterceptors><refbean="outLoggingInterceptor"/></jaxws:outInterceptors><jaxws:features><refbean="loggingFeature"/><wsa:addressingxmlns:wsa="http://cxf.apache.org/ws/addressing"/></jaxws:features></jaxws:client></beans>
说明:
这里的address需要填写服务的wsdl绝对地址,因此这里就是:http://localhost:8080/CXFDemo/soap/sayHello?wsdl
(5)测试
直接使用“Java Application”运行ClientDemo,输出如下:
---------------------------ID:1Address:http://localhost:8080/CXFDemo/soap/sayHello?wsdlEncoding:UTF-8Http-Method:POSTContent-Type:text/xmlHeaders:{Accept=[*/*],SOAPAction=[""]}Payload:<soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><Actionxmlns="http://www.w3.org/2005/08/addressing">http://zifangsky.cn/CXFService/sayHello</Action><MessageIDxmlns="http://www.w3.org/2005/08/addressing">urn:uuid:0d855e41-d638-4525-a3cf-8055c40f8605</MessageID><Toxmlns="http://www.w3.org/2005/08/addressing">http://localhost:8080/CXFDemo/soap/sayHello?wsdl</To><ReplyToxmlns="http://www.w3.org/2005/08/addressing"><Address>http://www.w3.org/2005/08/addressing/anonymous</Address></ReplyTo></soap:Header><soap:Body><ns2:sayHelloxmlns:ns2="http://zifangsky.cn/"><name>javaee</name></ns2:sayHello></soap:Body></soap:Envelope>--------------------------------------三月14,20164:51:41下午org.apache.cxf.services.CXFServiceService.CXFServicePort.CXFService信息:InboundMessage----------------------------ID:1Response-Code:200Encoding:UTF-8Content-Type:text/xml;charset=UTF-8Headers:{content-type=[text/xml;charset=UTF-8],Date=[Mon,14Mar201608:51:41GMT],Server=[Apache-Coyote/1.1],transfer-encoding=[chunked]}Payload:<soap:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><Actionxmlns="http://www.w3.org/2005/08/addressing">http://zifangsky.cn/CXFService/sayHelloResponse</Action><MessageIDxmlns="http://www.w3.org/2005/08/addressing">urn:uuid:504f4c19-5df9-42bd-bb02-761da4932490</MessageID><Toxmlns="http://www.w3.org/2005/08/addressing">http://www.w3.org/2005/08/addressing/anonymous</To><RelatesToxmlns="http://www.w3.org/2005/08/addressing">urn:uuid:0d855e41-d638-4525-a3cf-8055c40f8605</RelatesTo></soap:Header><soap:Body><ns2:sayHelloResponsexmlns:ns2="http://zifangsky.cn/"><return>Hello,javaee测试</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>--------------------------------------三月14,20164:51:41下午org.springframework.context.support.AbstractApplicationContextdoClose信息:Closingorg.springframework.context.support.ClassPathXmlApplicationContext@62a76581:startupdate[MonMar1416:51:39GMT+08:002016];rootofcontexthierarchyResponse:Hello,javaee测试
可以发现,最后成功输出了
Response:Hello,javaee测试
因为我们在客户端中只引用了CXFService.java这个接口,但是却输出了预期的内容,说明客户端的确是调用了服务端的服务并成功返回信息的
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。