最新項目中須要用到webservices發佈接口,以前項目用的都是xfire,因爲框架中Spring版本緣由,xfire用不起來了,而後去搜了下,開源中國裏找到了這段話:javascript
若是你要在新項目裏用XFire,請改用CXF.CXF是XFire的延續,能夠被看做是XFire2.0。CXF有不少新特性,且修復了不少XFire的bug,更重要的是CXF兼容JAX-WS!XFire將繼續被維護和修復bug,可是新功能的開發都轉移到了CXF上。詳細信息請參考XFire/Celtix合併聲明和CXF官網html
才發現xfire早在幾年前就中止更新啦,轉而替代產品是CXF(忽然發現公司框架好老土!),學習了下CXF,發現確實好用,配置簡單,調用方便,具體貌似還有一些強大的功能,暫時沒用到,不去深究,下面來簡單介紹下CXF的具體使用。java
1、CXF項目下載web
一、首先CXF的官網地址 : http://cxf.apache.org/
注:當前最新版本是3.1.2,我用的是milestone版本3.0.0spring
二、下載相關壓縮包(大約40-50M),解壓縮到本地,這邊主要用到裏面的jar,固然不下載也行,關鍵方便學習嘛apache
2、項目集成api
一、項目所需jar包(倆種方式)瀏覽器
第一種方式: 能夠直接經過maven配置下載,具體配置以下:markdown
<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包以下:app
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
二、服務端webservices接口發佈
首先配置web.xml
<!-- 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):
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):
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):
<?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>
發佈完成後要進行測試發佈接口是否成功,具體步驟以下:
一、根據web.xml中配置,在瀏覽器中輸入webservice發佈連接
例如http://localhost:8080/jssc_platform/ws
出現接口頁面:
二、點擊方法連接進入wsdl文件
如今服務端發佈到此結束,下面就開始有客戶端進行調用啦,調用方式有兩種,一種是通用調用方式,一種是代理調用
通用調用方式:
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]); }
代理調用方式:
一、打開CMD命令行窗口,切換到apache-cxf-3.0.0/bin目錄下,輸入命令:
wsdl2java -p cn.com.webxml -d f:/ http://localhost:8080/ws//getInfoService?wsdl
注:-p 表示生成的JAVA客戶端調用代碼的包路徑;-d 表示生成在本地哪一個目錄下(本例爲F盤根目錄); 最後一個參數爲WSDL文件路徑(也能夠是本地文件)
二、利用生成的工具類,直接調用webservices接口
UserInfoService_Service service = new UserInfoService_Service(); UserInfoService servicesop = service.getUserInfoServiceImplPort(); String userName = servicesop.getUser();
針對這倆種調用方式,我比較喜歡和經常使用的方式是第二種,這樣能夠根據用戶提供的webservices接口直接生成本地接口文件,調用很是方便。
以上就是CXF應用的簡單小結,我的認爲比以前的Xfire好用多啦!