CXF 教程(一)

CXF Web Service 简单示例html


1 准备工做

2 第一个例子

3 客户端

        3.1 使用 WSDL 生成客户端

4 RPC 风格

5 相关命令介绍

        5.1 Java to WS


1 准备工做

2 第一个例子

步骤以下java

  1. 建立 Maven 项目, 使用 quickstart 模板web

  2. 在 pom.xml 中引入依赖包,以下所示apache

    pom.xmlapi

    <properties>
    	<cxf.version>3.1.0</cxf.version> </properties> <dependencies> <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-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>javax.xml</groupId> <artifactId>webservices-api</artifactId> <version>2.0.1</version> </dependency> </project> 
  3. 写一个简单的 Service 类,以下所示浏览器

    HelloService.javaapp

    package lld.cxf.service; import javax.jws.WebService; @WebService public interface HelloService { String sayHi(String name); } 

     

    HelloServiceImpl.javafrontend

    package lld.cxf.service; public class HelloServiceImpl implements HelloService { public String sayHi(String name) { return "Hello " + name; } }
  4. 建立 Server 以下所示, 这就是为何需引入 cxf-rt-transports-http-jetty 包的缘由,CXF 内嵌了 Jetty server。ide

    HelloServer.javaui

    package lld.cxf.service; import org.apache.cxf.frontend.ServerFactoryBean; public class HelloServer { public static void main(String[] args) { // Create our service implementation HelloService helloWorldImpl = new HelloServiceImpl(); // Create our Server ServerFactoryBean svrFactory = new ServerFactoryBean(); svrFactory.setServiceClass(HelloService.class); svrFactory.setAddress("http://localhost:9000/Hello"); svrFactory.setServiceBean(helloWorldImpl); svrFactory.create(); } } 
  5. 运行 Server 后可在浏览器中输入 http://localhost:9000/Hello?wsdl 验证

  6. 建立 Client 端以下所示

3 客户端

3.1 使用 WSDL 生成客户端

上例中咱们直接把客户端和服务端放在了一个项目中,实际状况通常不会这样。一般是服务端发布 WSDL 的 URL,客户端使用 WSDL 来生成本地 Proxy 代码并访问 Web Service。

  1. 首先咱们得生成 WSDL 文件,最省事的办法是直接在浏览器中访问上例中的 WSDL 连接并把浏览器中的文本结果另存为本地文件并以 wsdl 做为扩展名

    或者咱们也能够根据编译结果生成 wsdl 文件。在下载的 CXF 中,在 bin 目录下找到 java2ws 命令,进入结果文件根目录(classes 目录),运行命令以下所示:

    java2ws -wsdl -o HelloService.wsdl lld.cxf.service.HelloService

    将会在当前目录生成 HelloService.wsdl

  2. 根据 wsdl 文件生成客户端 stub,一样是使用 CXF 下载包中的 wsdl2java 命令,以下所示

    wsdl2java -client -d ClientDir ../resources/HelloService.wsdl

    将把 Stub 生成在当前目录的 ClientDir 目录下

  3. 上一步生成的 Stub 中里面包含了不少文件,细节先不用管,把这些文件复制到当前源代码目录中,其中有一个文件 HelloServicePortType_HelloServicePort_Client.java 是一个客户端的调用示例文件,可参考里面的内容写出以下的客户端调用

    我将生成的 HelloService.wsdl 文件放在了 resources 目录下,也就是会自动复制到 classes 根目录下。

    HelloService.java

    package lld.cxf.client.test; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import lld.cxf.service.HelloService; import lld.cxf.service.HelloServicePortType; public class HelloClientTest { private static final QName SERVICE_NAME = new QName("http://service.cxf.lld/", "HelloService"); public static void main(String[] args) throws MalformedURLException { String wsdlFileName = "HelloService.wsdl"; URL wsdlURL = HelloClientTest.class.getClassLoader().getResource(wsdlFileName); HelloService ss = new HelloService(wsdlURL, SERVICE_NAME); HelloServicePortType port = ss.getHelloServicePort(); System.out.println("Invoking sayHi..."); String result = port.sayHi("Lindong"); System.out.println("sayHi.result=" + result); } } 
  4. 另外也能够没必要将 wsdl 存放在本地而是直接从远端获取,将上面获取 URL 的代码进行以下替换便可:

    String wsdlUrl = "http://localhost:9000/Hello?wsdl"; URL wsdlURL = new URL(wsdlUrl); 

一般状况下,若是公司不是按代码量算薪水,咱们通常会将 Stub 类打成 jar 包放在引用路径里,以使代码更加清晰。若是使用 Eclipse,可直接使用 Export 功能将选中的 Stub package 导出为jar 包。

4 RPC 风格

将上面的 Service 类修改以下,将生成 RPC 风格的 Web Service

HelloService.java

package lld.cxf.service; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style = Style.RPC) public class HelloService { public String sayHi(String name) { return "Hello " + name; } } 

5 相关命令介绍

5.1 Java to WS

5.1.1 Overview

官方帮助:Java to WS

在 2.1 之前的版本中命令为 java2wsdl,在新的版本中为 java2ws

java2ws 用于生成 Web service endpoint's implementation (SEI) 类并根据这些类生成 WSDL 文件, Bean 封装类, 用于启动服务的服务端代码和客户端方问代码。

5.1.2 语法

语法以下所示:

java2ws -databinding  -frontend  
        -wsdl -wrapperbean -client -server -ant -o 
        -d  -classdir  
        -cp  -soap12 -t  
        -beans * 
        -address  -servicename  
        -portname  -createxsdimports -h -v -verbose 
        -quiet {classname}

5.1.3 参数说明

各参数说明以下:

Option

Interpretation

-?,-h,-help

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.

5.1.4 示例

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 

5.1.5 与 Ant 集成

<?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>
相关文章
相关标签/搜索