CXF Web Service 簡單示例html
步驟以下java
建立 Maven 項目, 使用 quickstart 模板web
在 pom.xml 中引入依賴包,以下所示apache
寫一個簡單的 Service 類,以下所示瀏覽器
建立 Server 以下所示, 這就是爲何需引入 cxf-rt-transports-http-jetty 包的緣由,CXF 內嵌了 Jetty server。ide
運行 Server 後可在瀏覽器中輸入 http://localhost:9000/Hello?wsdl 驗證
建立 Client 端以下所示
上例中咱們直接把客戶端和服務端放在了一個項目中,實際狀況通常不會這樣。一般是服務端發佈 WSDL 的 URL,客戶端使用 WSDL 來生成本地 Proxy 代碼並訪問 Web Service。
首先咱們得生成 WSDL 文件,最省事的辦法是直接在瀏覽器中訪問上例中的 WSDL 連接並把瀏覽器中的文本結果另存爲本地文件並以 wsdl 做爲擴展名
或者咱們也能夠根據編譯結果生成 wsdl 文件。在下載的 CXF 中,在 bin 目錄下找到 java2ws 命令,進入結果文件根目錄(classes 目錄),運行命令以下所示:
java2ws -wsdl -o HelloService.wsdl lld.cxf.service.HelloService
將會在當前目錄生成 HelloService.wsdl
根據 wsdl 文件生成客戶端 stub,一樣是使用 CXF 下載包中的 wsdl2java 命令,以下所示
wsdl2java -client -d ClientDir ../resources/HelloService.wsdl
將把 Stub 生成在當前目錄的 ClientDir 目錄下
上一步生成的 Stub 中裏面包含了不少文件,細節先不用管,把這些文件複製到當前源代碼目錄中,其中有一個文件 HelloServicePortType_HelloServicePort_Client.java 是一個客戶端的調用示例文件,可參考裏面的內容寫出以下的客戶端調用
我將生成的 HelloService.wsdl 文件放在了 resources 目錄下,也就是會自動複製到 classes 根目錄下。
另外也能夠沒必要將 wsdl 存放在本地而是直接從遠端獲取,將上面獲取 URL 的代碼進行以下替換便可:
String wsdlUrl = "http://localhost:9000/Hello?wsdl"; URL wsdlURL = new URL(wsdlUrl);
一般狀況下,若是公司不是按代碼量算薪水,咱們通常會將 Stub 類打成 jar 包放在引用路徑裏,以使代碼更加清晰。若是使用 Eclipse,可直接使用 Export 功能將選中的 Stub package 導出爲jar 包。
將上面的 Service 類修改以下,將生成 RPC 風格的 Web Service
官方幫助:Java to WS
在 2.1 之前的版本中命令爲 java2wsdl,在新的版本中爲 java2ws
java2ws 用於生成 Web service endpoint's implementation (SEI) 類並根據這些類生成 WSDL 文件, Bean 封裝類, 用於啓動服務的服務端代碼和客戶端方問代碼。
語法以下所示:
java2ws -databinding -frontend -wsdl -wrapperbean -client -server -ant -o -d -classdir -cp -soap12 -t -beans * -address -servicename -portname -createxsdimports -h -v -verbose -quiet {classname}
各參數說明以下:
Option |
Interpretation |
---|---|
|
Displays the online help for this utility and exits. |
-o |
Specifies the name of the generated WSDL file. |
--databinding |
Specify the data binding (aegis or jaxb). Default is jaxb for jaxws frontend, and aegis for simple frontend. |
-frontend |
Specify the frontend to use. jaxws and the simple frontend are supported. |
-wsdl |
Specify to generate the WSDL file. |
-wrapperbean |
Specify to generate the wrapper and fault bean |
-client |
Specify to generate client side code |
-server |
Specify to generate server side code |
-ant |
Specify to generate an Ant build.xml script |
-cp |
Specify the SEI and types class search path of directories and zip/jar files. |
-soap12 |
Specifies that the generated WSDL is to include a SOAP 1.2 binding. |
-t |
Specifies the target namespace to use in the generated WSDL file. |
-servicename |
Specifies the value of the generated service element's name attribute. |
-v |
Displays the version number for the tool. |
-verbose |
Displays comments during the code generation process. |
-quiet |
Suppresses comments during the code generation process. |
-s |
The directory in which the generated source files(wrapper bean ,fault bean ,client side or server side code) are placed. |
-classdir |
The directory in which the generated sources are compiled into. If not specified, the files are not compiled. |
-portname |
Specify the port name to use in the generated wsdl. |
-address |
Specify the port address. |
-beans |
Specify the pathname of a file defining additional Spring beans to customize databinding configuration. |
-createxsdimports |
Output schemas to separate files and use imports to load them instead of inlining them into the wsdl. |
-d |
The directory in which the resource files are placed, wsdl file will be placed into this directory by default |
classname |
Specifies the name of the SEI class. |
java2ws -wsdl -d ./resources lld.cxf.service.HelloService java2wsdl -cp ./tmp org.apache.hello_world_soap_http.Greeter java2wsdl -o hello.wsdl org.apache.hello_world_soap_http.Greeter java2wsdl -o hello.wsdl -t http://cxf.apache.org org.apache.hello_world_soap_http.Greeter
<?xml version="1.0"?> <project name="cxf java2ws" basedir="."> <property name="cxf.home" location ="/usr/myapps/cxf-trunk"/> <property name="build.classes.dir" location ="${basedir}/build/classes"/> <path id="cxf.classpath"> <pathelement location="${build.classes.dir}"/> <fileset dir="${cxf.home}/lib"> <include name="*.jar"/> </fileset> </path> <target name="cxfJavaToWS"> <java classname="org.apache.cxf.tools.java2ws.JavaToWS" fork="true"> <arg value="-wsdl"/> <arg value="-o"/> <arg value="hello.wsdl"/> <arg value="service.Greeter"/> <classpath> <path refid="cxf.classpath"/> </classpath> </java> </target> </project>