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>