最新项目中需要用到webservices发布接口,之前项目用的都是xfire,由于框架中Spring版本原因,xfire用不起来了,然后去搜了下,开源中国里找到了这段话:
如果你要在新项目里用XFire,请改用CXF.CXF是XFire的延续,可以被看作是XFire2.0。CXF有很多新特性,且修复了很多XFire的bug,更重要的是CXF兼容JAX-WS!XFire将继续被维护和修复bug,但是新功能的开发都转移到了CXF上。详细信息请参考XFire/Celtix合并声明和CXF官网
才发现xfire早在几年前就停止更新啦,转而替代产品是CXF(突然发现公司框架好老土!),学习了下CXF,发现确实好用,配置简单,调用方便,具体貌似还有一些强大的功能,暂时没用到,不去深究,下面来简单介绍下CXF的具体使用。
一、CXF项目下载 1、首先CXF的官网地址 : http://cxf.apache.org/ 注:当前最新版本是3.1.2,我用的是milestone版本3.0.0
2、下载相关压缩包(大约40-50M),解压缩到本地,这边主要用到里面的jar,当然不下载也行,关键方便学习嘛
二、项目集成 1、项目所需jar包(俩种方式) 第一种方式: 可以直接通过maven配置下载,具体配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 <cxf.version>3.0.0-milestone1 </cxf.version> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-common</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-java2ws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-validator</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-wsdlto-core</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-wsdlto-databinding-jaxb</artifactId> <version>${cxf.version}</version> </dependency>
第二种方式,直接通过下载下的压缩包,解压获取相关jar包,具体使用到的jar包如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 cxf-core-3.0.0-milestone1.jar cxf-rt-bindings-soap-3.0.0-milestone1.jar cxf-rt-bindings-xml-3.0.0-milestone1.jar cxf-rt-databinding-aegis-3.0.0-milestone1.jar cxf-rt-databinding-jaxb-3.0.0-milestone1.jar cxf-rt-frontend-jaxws-3.0.0-milestone1.jar cxf-rt-frontend-simple-3.0.0-milestone1.jar cxf-rt-javascript-3.0.0-milestone1.jar cxf-rt-transports-http-3.0.0-milestone1.jar cxf-rt-ws-addr-3.0.0-milestone1.jar cxf-rt-wsdl-3.0.0-milestone1.jar cxf-rt-ws-policy-3.0.0-milestone1.jar cxf-tools-common-3.0.0-milestone1.jar cxf-tools-java2ws-3.0.0-milestone1.jar cxf-tools-validator-3.0.0-milestone1.jar cxf-tools-wsdlto-core-3.0.0-milestone1.jar cxf-tools-wsdlto-databinding-jaxb-3.0.0-milestone1.jar cxf-tools-wsdlto-frontend-jaxws-3.0.0-milestone1.jar neethi-3.0.2.jar stax2-api-3.1.1.jar woodstox-core-asl-4.2.0.jar wsdl4j-1.6.3.jar xml-apis-1.0.b2.jar xml-resolver-1.2.jar xmlschema-core-2.0.3.jar
2、服务端webservices接口发布 首先配置web.xml1 2 3 4 5 6 7 8 9 10 <!-- CXF 配置 --> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/ ws/*</url-pattern> </servlet-mapping>
然后编写业务接口(UserInfoService):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.quangao.service.webservice; import javax.jws.WebService; /** * Business Service Interface to handle communication between web and * persistence layer. * */ @WebService public interface UserInfoService { String getUser(); }
有接口必然要有接口实现(UserInfoServiceImpl):
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.quangao.service.webservice.impl; import javax.jws.WebService; import com.quangao.service.webservice.UserInfoService; @WebService(endpointInterface = "com.quangao.service.webservice.UserInfoService", targetNamespace = " http://webservice.service.quangao.com/" public class UserInfoServiceImpl implements UserInfoService { public String getUser() { return " 钱猛 "; } }
接口写好之后就可以发布接口啦,具体配置如下(application.xml):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <?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:p=" http://www.springframework.org/schema/p " xmlns:tx=" http://www.springframework.org/schema/tx " xmlns:aop=" http://www.springframework.org/schema/aop " xmlns:jaxws=" http://cxf.apache.org/jaxws " xmlns:cxf=" http://cxf.apache.org/core " xsi:schemaLocation= " http://www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/sprin g-beans-3.0.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/sprin g-tx-3.0.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/sprin g-aop-3.0.xsd http: //cxf.apache.org/jaxws http: //cxf.apache.org/schemas/jaxws.xsd" > <!-- 配置请参考官网: http://cxf.apache.org/docs/jax-rs-and-jax-ws.html -- <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="getInfoServiceImpl" class="com.quangao.service.webservice.impl.UserInfoServiceImpl" /> <!-- JAX-WS --> <!-- implementor 指定 WebService 实现类, address 指定访问地址 --> <jaxws:endpoint id="getInfoService" implementor="#getInfoServiceImpl" address="/getInfoService" publish="true" /> </beans>
发布完成后要进行测试发布接口是否成功,具体步骤如下: 1、根据web.xml中配置,在浏览器中输入webservice发布链接 例如http://localhost:8080/jssc_platform/ws 出现接口页面:
2、点击方法链接进入wsdl文件
现在服务端发布到此结束,下面就开始有客户端进行调用啦,调用方式有两种,一种是通用调用方式,一种是代理调用
通用调用方式: 1 2 3 4 5 6 7 8 9 10 11 12 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); // 方式1. 指定WSDL文件的url地址 org.apache.cxf.endpoint.Client client = dcf .createClient("http://localhost:8080/jssc-web/ws/getInfoService?wsdl"); // 方式2. 指定WSDL文件的绝对路径 // org.apache.cxf.endpoint.Client client = dcf.createClient("E:/cxf/getInfoService.wsdl"); // 方式3. classpath下的WSDL文件 // org.apache.cxf.endpoint.Client client = dcf.createClient("getInfoService.wsdl"); Object[] info = client.invoke("getUser"); if (info != null) { System.out.println("cxf 返回结果:" + info[0]); }
代理调用方式: 1、打开CMD命令行窗口,切换到apache-cxf-3.0.0/bin目录下,输入命令:
1 wsdl2java -p cn.com.webxml -d f:/ http://localhost:8080/ws//getInfoService?wsdl
注:-p 表示生成的JAVA客户端调用代码的包路径;-d 表示生成在本地哪个目录下(本例为F盘根目录); 最后一个参数为WSDL文件路径(也可以是本地文件)
2、利用生成的工具类,直接调用webservices接口
1 2 3 UserInfoService_Service service = new UserInfoService_Service(); UserInfoService servicesop = service.getUserInfoServiceImplPort(); String userName = servicesop.getUser();
针对这俩种调用方式,我比较喜欢和常用的方式是第二种,这样可以根据用户提供的webservices接口直接生成本地接口文件,调用非常方便。
以上就是CXF应用的简单小结,个人认为比之前的Xfire好用多啦!