最近公司業務上使用webservice 頻繁。因爲以前都是本身搭建webservice 本身定義提供給別人服務,如今則相反需求都是根據人家提供的wsdl 文件來生成 咱們平臺須要提供的接口。剛開始不知道如何生成,爲了一個webservice 服務,而搭建多種環境如: xfire,jaxws,axis,cxf , 幾種都搭建起來,一種一種比較生成wsdl 的格式,最好確認了格式,又得確認方法,對象,服務名,一系列下來的花上 1-2天時間。java
言歸正傳,首先普及一下,webservice 規範, 不管它們所使用的語言、 平臺或內部協議是什麼, 均可以相互交換數據。web
換句話說,人家提供的wsdl ,咱們生成的基本條件爲 調用的服務名,端口,方法名得和 提供的wsdl 一致,這樣才能通信. 因此沒必要糾結使用那個框架 生成,選咱們最熟悉的webservice ,這裏我使用的是 apache-cxf-2.7.11spring
生成 根據人家提供的wsdl 同樣的接口與服務 , 測試服務接口軟件 soapui-4.5.2 , apache-cxf-2.7.11apache
1. 在官網下載 apache-cxf-2.7.11 完成後,配置環境變量(和java 配置環境變量同樣)
api
新建環境變量CXF_HOME = 你的路徑/apache-cxf-2.7.11瀏覽器
2. 在Path 後面添加 %CXF_HOME%/bin ,保存退出mybatis
3. cmd 一下 看是否有此界面,若是有則成功了mvc
4. 啓動 soapui-4.5.2\bin\soapui.bat , 新建 New soapUI projectapp
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
顯示以下。成功