教程(2)依据教程(1)中提供的WSDL契约,对其发布的webService创建了一个简单的客户端;本文详细介绍一下webService客户端开发的一般步骤。
生成Stub代码
在CXF中,开发一个service消费者(或客户端)的起点是一个WSDL契约,连同端口类型、绑定以及service定义。然后我们就可以使用 wsdl2java 工具来根据WSDL契约生成Java stub 代码。stub代码提供了调用远端服务方法所需的支持代码。
wsdl2java 工具可以为CXF客户端生成下列代码:
- Stub code - 实现一个CXF客户端需要的支持文件。
- Client starting point code - 样例客户端代码,可以连到远程服务并调用每一个操作。
- Ant build file - 一个ant构建工具可以使用的 build.xml 文件,其中包含构建和运行样例程序的targets。
基本的 HelloWorld WSDL 契约
下面是HelloWorld WSDL 契约的示例。该契约定义了一个单一的端口类型,Greeter;一个SOAP绑定,Greater_SOAPBinding;以及一个服务,SOAPService,它有一个单独的端口,SoapPort。
HelloWorld WSDL Contract
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="HelloWorld" targetNamespace="http://apache.org/hello_world_soap_http"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://apache.org/hello_world_soap_http"
xmlns:x1="http://apache.org/hello_world_soap_http/types"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<schema targetNamespace="http://apache.org/hello_world_soap_http/types"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://apache.org/hello_world_soap_http/types"
elementFormDefault="qualified">
<simpleType name="MyStringType">
<restriction base="string">
<maxLength value="30" />
</restriction>
</simpleType>
<element name="sayHi">
<complexType/>
</element>
<element name="sayHiResponse">
<complexType>
<sequence>
<element name="responseType" type="string"/>
</sequence>
</complexType>
</element>
<element name="greetMe">
<complexType>
<sequence>
<element name="requestType" type="tns:MyStringType"/>
</sequence>
</complexType>
</element>
<element name="greetMeResponse">
<complexType>
<sequence>
<element name="responseType" type="string"/>
</sequence>
</complexType>
</element>
<element name="greetMeOneWay">
<complexType>
<sequence>
<element name="requestType" type="string"/>
</sequence>
</complexType>
</element>
<element name="pingMe">
<complexType/>
</element>
<element name="pingMeResponse">
<complexType/>
</element>
<element name="faultDetail">
<complexType>
<sequence>
<element name="minor" type=