最近公司業務上使用webservice 頻繁。由於之前都是自己搭建webservice 自己定義提供給別人服務,現在則相反需求都是根據人家提供的wsdl 文件來生成 我們平台需要提供的接口。剛開始不知道如何生成,為了一個webservice 服務,而搭建多種環境如: xfire,jaxws,axis,cxf , 幾種都搭建起來,一種一種比較生成wsdl 的格式,最好確認了格式,又得確認方法,對象,服務名,一系列下來的花上 1-2天時間。
言歸正傳,首先普及一下,webservice 規范, 無論它們所使用的語言、 平台或內部協議是什么, 都可以相互交換數據。
換句話說,人家提供的wsdl ,我們生成的基本條件為 調用的服務名,端口,方法名得和 提供的wsdl 一致,這樣才能通訊. 所以不必糾結使用那個框架 生成,選我們最熟悉的webservice ,這里我使用的是 apache-cxf-2.7.11
生成 根據人家提供的wsdl 一樣的接口與服務 , 測試服務接口軟件 soapui-4.5.2 , apache-cxf-2.7.11
apache-cxf-2.7.11 環境變量配置
1. 在官網下載 apache-cxf-2.7.11 完成后,配置環境變量(和java 配置環境變量一樣)
新建環境變量CXF_HOME = 你的路徑/apache-cxf-2.7.11
2. 在Path 后面添加 %CXF_HOME%/bin ,保存退出
3. cmd 一下 看是否有此界面,如果有則成功了
使用apache-cxf創建soapUI並創建webService服務
4. 啟動 soapui-4.5.2\bin\soapui.bat , 新建 New soapUI project
5. 點ok 后, 生成如下圖,然后選中項目選擇apache cxf
6. 點擊tools,選中cxf 路徑,ok后,在右邊礦 選擇輸出文件路徑,填寫包路徑,勾選 生成 generates client ,generates server, generates implement, 點擊generates
7. 這就完成了。其實soapUI 也只是用apache-cxf 命令生成。也可以用apache-cxf bin 下生成目錄.
8. 引入jar 包 ,我這里使用的是 maven
<dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-api</artifactId> <version>2.7.11</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.11</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-soap</artifactId> <version>2.7.11</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.11</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>2.7.11</version> </dependency>
9. 復制到項目后。 新建文件名applicationContext-cxf.xml ,
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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-servlet.xml" /> <jaxws:endpoint id="sptsmstubws" implementor="com.ishua.tsmsp.service.SptsmstubwsImpl" address="/sptsmstubws" /> </beans>
10. 與spring 的applicationContext.xml 文件一個目錄, 再在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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " > <import resource="applicationContext-cxf.xml"/>
11. 在工程里配置 web.xml
<!--cxf webservice --> <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>/services/*</url-pattern> </servlet-mapping>
完成上面步驟之前首先你的ssh 工程得跑的起來。最少得有spring 支持。我使用的是 spring mvc + mybatis
按上面來不會有錯誤. 在瀏覽器輸入 http://localhost:8080/tsmweb/services/sptsmstubws?wsdl
路徑名稱 servlet url pattern + applicationContext-cxf.xml address
顯示如下。成功