转自:http://wocclyl.blog.163.com/blog/static/46223504200843054022716/
本文介绍了如何编写一个简单的WSDL文件!
首先明确的一点是WSDL现在有两个版本,分别为WSDL 1.1和WSDL 2.0,W3C的官方文档地址分别为:
Web Services Descrīption Language (WSDL) 1.1
W3C Note 15 March 2001
和
Web Services Descrīption Language (WSDL) Version 2.0 Part 0: Primer
W3C Working Draft 26 March 2007
其中很多应用还是以版本1.1为基础实现。下面是2.0与1.1的区别:
Adding further semantics to the descrīption language.
Removal of message constructs.
No support for operator overloading.
PortTypes renamed to interfaces.
Ports renamed to endpoints.
下面是一些常见的命名空间:
prefix namespace URI
wsdl http://schemas.xmlsoap.org/wsdl/
soap http://schemas.xmlsoap.org/wsdl/soap/
http http://schemas.xmlsoap.org/wsdl/http/
mime http://schemas.xmlsoap.org/wsdl/mime/
soapenc http://schemas.xmlsoap.org/soap/encoding/
soapenv http://schemas.xmlsoap.org/soap/envelope/
xsi http://www.w3.org/2000/10/XMLSchema-instance
xsd http://www.w3.org/2000/10/XMLSchema
tns (various)
对于WSDL规范,可以参考以上两个官方文档,本文主要介绍如何编写WSDL文档(其实官方文档中已经给出了很多例子和方法,这里只是简单的翻译与重复介绍)。
下面举例说明如何编写WSDL文档:
我们做一个非常简单的加法运算服务:客户端传入SOAP请求消息,包含两个加数,然后在服务器端获取这两个加数,求和,然后返回给客户端。
请求消息和响应消息结构分别如下(有效负载部分,外层的SOAP封装AXIS2会自动添加上去):
request:
<SumRequest>
<First>15</First>
<Second>16</Second>
</SumRequest>
response:
<SumResponse>
<Result>31</Result>
</SumResponse>
1.首先需要进行定义的就是soap消息的数据类型,无疑,这里需要定义两个复杂的数据类型:
SumRequest
SumResponse
它们分别包含有子元素First、Second和Result.
另外还需要定义一种异常,这里我们定义成为SumFault,比如传入的两个加数的和超出了xsd:int的范围时,抛出该异常。
(注意,很多代码生成器都会根据WSDL生成代码,将SumFault部分生成为后缀为Exception的异常类。)
2.定义数据类型的目的是为传入传出消息做准备的,传入传出消息的定义方式使用message元素来定义。
我们定义三种消息:SumRequest,SumResponse以及SumFault,分别为请求消息,响应消息以及出错时的消息。
3.定义好了传入传出消息后,就要定义一个portType,该节点类型定义了主要的业务操作。
4.接着将定义SOAP绑定:
SumSoapBinding:为SumService端口类型所定义的操作和消息指定具体传输中所使用的消息格式和协议细节。绑定的方式为SOAP,传输方式为http,消息的格式为document。
5.定义Web服务:Web服务名为SumService。
6.本Web服务的操作为Sum操作。
7.端口为:为SumSoapBing绑定指定一个地址来定义服务访问点。
根据以上七个部分编写完成的WSDL文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.zzl.org/Sum"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.zzl.org/Sum">
<wsdl:documentation>
The WSDL file of SumService.
</wsdl:documentation>
<wsdl:types>
<wsdl:documentation>
Data types that are used for request and response messages.
</wsdl:documentation>
<xsd:schema targetNamespace="http://www.zzl.org/Sum">
<xsd:element name="SumRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="First" type="xsd:int" />
<xsd:element name="Second" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SumResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Result" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SumFault">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Code" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="SumRequest">
<wsdl:documentation>
The data that will be transmitted to the service.
</wsdl:documentation>
<wsdl:part element="tns:SumRequest" name="request" />
</wsdl:message>
<wsdl:message name="SumResponse">
<wsdl:documentation>
The data that will be returned to the client.
</wsdl:documentation>
<wsdl:part element="tns:SumResponse" name="response" />
</wsdl:message>
<wsdl:message name="SumFault">
<wsdl:documentation>
The fault that will be thrown when fault occurs.
</wsdl:documentation>
<wsdl:part name="axisFault" element="tns:SumFault" />
</wsdl:message>
<wsdl:portType name="SumService">
<wsdl:documentation>
The SumService contains the business operation.
</wsdl:documentation>
<wsdl:operation name="RevokeCert">
<wsdl:documentation>
The operation that do the business work.
</wsdl:documentation>
<wsdl:input message="tns:SumRequest" />
<wsdl:output message="tns:SumResponse" />
<wsdl:fault name="fault" message="tns:SumFault" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="SumSoapBinding" type="tns:SumService">
<wsdl:documentation>
The SumSoapBinding defines the SOAP message format and
protocol details for Sum operation and messages defined by a
RevokeService portType.
</wsdl:documentation>
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="Sum">
<soap:operation soapAction="urn:Sum" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
<wsdl:fault name="fault">
<soap:fault use="literal" name="fault" />
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="SumService">
<wsdl:documentation>
SumService provides the service of summing.
</wsdl:documentation>
<wsdl:port binding="tns:SumSoapBinding" name="SumSoapBinding">
<wsdl:documentation>
The port defines the endpoint by specifying a soap
address for SumSoapBinding.
</wsdl:documentation>
<soap:address location="http://www.zzl.org/ExampleService/services/SumService" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
服务器端和客户端的代码是根据WSDL文件编写出来的,这才是正确的过程,但是,在国内的软件开发过程,常常是先写代码,再根据代码生成WSDL文件,这是不正确的。
关于如何生成客户端与服务端代码,可以参考相应的框架,如Axis2和CXF等,这里不再介绍。
分享到:
相关推荐
本文介绍了如何编写一个简单的WSDL文件,并根据WSDL文件编写服务器端和客户端代码,并发布Web Service服务的过程。
wsdl4j 解析wsdl 文件wsdl4j 解析wsdl 文件wsdl4j 解析wsdl 文件
把wsdl文件转换成java类 使用wsdl2Java工具
wsdl文件结构 元素的详细说明及其使用
根据WSDL文件生成客户端代码与创建客户端工程 注意小细节:在根据对方提供的webservice地址,先将对方webservice的wsdl文件描述文件保存到本地时,格式先保存为xml,后将其后缀名为.wsdl的文件 否则报:...
WebService的WSDL文件生成Java代码工具
Eclipse根据wsdl文件逆向生成webservice客户端项目源码,包括wsdl文件,测试示例,测试webservice代码,包含备注信息
最新联通VAC接口WSDL文件 VacSyncService_SPClient_20091212.wsdl.xml
在myeclipse下的一个web程序,把wsdl文件转化成java代码
onvif开发所有wsdl文件(修改过),下载后就可以直接用wsimport生成java本地代码
最新的wsdl文件。通过这些文件可以生成最新的完整的onvif协议开发代码。
一个wsdl文件,用axis2解析,一个wsdl文件,用axis2解析,
onvif wsdl文件,并且带了gsoap生成脚本
通过wsdl文件就可以通过客户端调用服务端的接口,可以使用Webservice的服务了
onvif的wsdl文件2016
WSDL文件结构详解 WSDL文件结构详解 WSDL文件结构详解 WSDL文件结构详解
详细描述了wsdl文件的一些属性及其意义
gsoap移植之wsdl文件,IPCAM,网络摄像头
对webservic中 wsdl文件进行了详细的分析和介绍