恩,我想说的是,是不是常常有人在开发的时候,特别是和第三方有接口的时候,走的是SOAP协议,然后用户给你一个WSDL文件,说依照上面的进行适配,嘿嘿,这个时候,要是你曾经没有开发过,肯定会傻眼,那假设你想学习的话,就认真的看以下的解说咯:


一、WSDL概述
        WebServices Description Language (WSDL Web服务语言)是一个用于精确描写叙述Web Service的文档格式。
        WSDL很适合于用作代码生成器,它可以读取WSDL文档,而且可以为訪问Web服务生成一个程序化的接口,大多数软件供应商和基本的标准机构(包含 W3C、WS-I和OASIS)都支持WSDL。比如:JAX-RPC provider(比如:BEA Weblogic)通过API用WSDL生成对应的占位程序;IBM WebSphere、Microsoft.NET以及Apache Axis都有自己的工具生成相关的代码。下图是一个样例:
                                      

       上面的样例JAX-RPC通过读取WSDL文档,创建JAX-RPC RMI接口(endpoint接口)和实现此接口的网络占位程序(stub)。client程序通过RMI接口,Stub和Web Service服务端交换SAOP消息。

二、WSDL基本结构
        WSDL文档是一个遵循WSDL XML模式的XML文档(文档实例);相似于:SOAP文档是一个遵循SOAP XML模式的XML文档(文档实例);
        一个WSDL文档的根元素是definitions元素,WSDL文档包括7个重要的元素:types, import, message, portType, operations, binding和service元素。

三、WSDL声明
      
        3.1 XML声明


           <?xml version="1.0" encoding="UTF-8"?>

WSDL的声明必须定义成使用:UTF-8 或者UTF-16 编码。


3.2 definition元素
              全部WSDL文档的根元素都是definition元素。


<definitions name="BookQuoteWS"
                      targetNamespace="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:mh="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:soapbind="http://schemas.xmlsoap.org/wsdl/soap/"
                         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                     xmlns="http://schemas.xmlsoap.org/wsdl/">

 definition元素中一般包含若干个XML命名空间;
 http://schemas.xmlsoap.org/wsdl/是默认的命名空间,这样就能够不用显式地定义每个WSDL元素的命名空间了,比如:<types> <messages> <portType>…;文档中全部的元素缺省应该属于这个命名空间。
definition元素的的一个属性是name,此属性不重要能够没有;
   定义了targetNamespace命名空间,它为在模式中显式创建的全部新类型均声明了XML命名空间,并且上面的样例中赋予了mh前缀。

<!-- message elements describe the input and output parameters -->

<message name="GetBookPriceRequest">

      <part name="isbn" type="xsd:string" />

</message>

<message name="GetBookPriceResponse">

     <part name="price" type="xsd:float" />

</message>

<!-- portType element describes the abstract interface of a Web service -->

<portType name="BookQuote">

    <operation name="getBookPrice">

          <input name="isbn" message="mh:GetBookPriceRequest"/>

          <output name="price" message="mh:GetBookPriceResponse"/>

  </operation>

</portType>

上面的样例中:message元素利用name属性指定了标签(比如:GetBookPriceRequest),这些标签会自己主动使用targetNamespace的命名空间,标签了的messages元素通常被称为定义。
          文档中的其它元素用标签和命名空间前缀去应用定义,比如上面的样例中:input元素是使用mh:GetBookPriceRequest来引用标签GetBookPriceRequest。

         3.3 Types元素
               Types元素用作一个容器,定义了自己定义的特殊数据类型,在声明消息部分(有效负载)的时候,messages定义使用了types元素中定义的数据类型与元素。


<?xml version="1.0" encoding="UTF-8"?>

<definitions name="BookQuoteWS"
                      targetNamespace="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:mh="http://www.Monson-Haefel.com/jwsbook/BookQuote"
                      xmlns:soapbind="http://schemas.xmlsoap.org/wsdl/soap/"
                      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                     xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>

    <xsd:schema   targetNamespace="http://www.Monson-Haefel.com/jwsbook/BookQuote">

      <!-- The ISBN simple type -->

      <xsd:simpleType name="ISBN">

        <xsd:restriction base="xsd:string">

          <xsd:pattern value="[0-9]{9}[0-9Xx]" />

        </xsd:restriction>

      </xsd:simpleType>

    </xsd:schema>

</types>

Types元素作为一个容器,用来定义XML模式内置的数据类型(即复杂类型和定制的简单类现,具体见Web Service XML文章)中没有描写叙述的各种数据类型。比如:ISBN。
        上面的样例中,types元素中直接嵌套了一个完整的W3C XML模式文档,此文档中targetNamespace必须是一个有效的非空值,并且必须属于由WSDL文档。

      3.4 Import元素
            Import元素能够让当前的文档使用其它WSDL文档中指定命名空间中的定义。


<definitions name="AllMhWebServices"
         xmlns="http://schemas.xmlsoap.org/wsdl/">

    <import namespace="http://www.Monson-Haefel.com/jwsbook/BookQuote"

     location="http://www.Monson-Haefel.com/jwsbook/BookPrice.wsdl"/>

    <import namespace="http://www.Monson-Haefel.com/jwsbook/po"

     location="http://www.Monson-Haefel.com/jwsbook/wsdl/PurchaseOrder.wsdl"/>

    <import namespace="http://www.Monson-Haefel.com/jwsbook/Shipping"

     location="http://www.Monson-Haefel.com/jwsbook/wsdl/Shipping.wsdl"/>

</definitions >

WSDL的import元素必须声明两个属性,即namespace属性和location属性。
          namespace属性必须和正导入的WSDL文档中声明的targetNamespace相匹配。
          location属性必须指向一个实际的WSDL文档。

四、WSDL抽象接口
         Message、portType和operation元素用于描写叙述Web服务的抽象接口,相当于JAVA或者C++中编程中的类的接口。当中portType相当于类接口的名称;operation相当于接口中包括的函数,message相当于函数的參数和返回值。
       
        4.1 Message元素
              Message元素描写叙述了Web服务的有效负载。相当于函数调用中的參数和返回值。


<message name="GetBulkBookPriceRequest">

    <part name="isbn" type="xsd:string"/>

    <part name="quantity" type="xsd:int"/>

  </message>

  <message name="GetBulkBookPriceResponse">

    <part name="price" type="mh:prices" />

  </message>

RPC式样的Web服务的message服务

GetBulkBookPriceRequest表示消息的输入(相当于函数的參数),GetBulkBookPriceResponse表示消息的输出(相当于函数的返回值)

Web Service的输入和输出參数能够是多个(一个特点),每个输入或者输出使用part元素定义,RPC样式必须使用type来定义类型

RPC样式用类型来数据定义过程调用,调用中的每个元素表示某一个类型的參数。

<types>

    <xsd:schema targetNamespace="http://www.Monson-Haefel.com/jwsbook/PO">

      <!-- Import the PurchaseOrder XML schema document -->

      <xsd:import namespace="http://www.Monson-Haefel.com/jwsbook/PO"

       schemaLocation="http://www.Monson-Haefel.com/jwsbook/po.xsd" />

    </xsd:schema>

  </types>

  <!-- message elements describe the input and output parameters -->

  <message name="SubmitPurchaseOrderMessage">

    <part name="order" element="mh:purchaseOrder" />

  </message>

文档式样Web服务的Messages元素:

当用户採用文档式样消息传递模式的时候,messages元素要应用types定义中的顶级元素。具体顶级元素的定义和XML schema详见Web Server XML文档。

消息部分使用element属性定义

文档式样的消息传递要交换XML文档,而且应用它们的顶级元素。

注:Messages元素的RPC/Document试样相应了SOAP RPC/Document消息传递模式,具体见Web Server SOAP相关文档

<types>

    <xsd:schema targetNamespace="http://www.Monson-Haefel.com/jwsbook/PO">

      <!-- Import the PurchaseOrder XML schema document -->

      <xsd:element name="InvalidIsbnFaultDetail" >

        <xsd:complexType>

          <xsd:sequence>

            <xsd:element name="offending-value" type="xsd:string"/>

            <xsd:element name="conformance-rules" type="xsd:string" />

          </xsd:sequence>

        </xsd:complexType>

      </xsd:element>

    </xsd:schema>

  </types>

 

  <!-- message elements describe the input and output parameters -->

  <message name="GetBookPriceRequest">

    <part name="isbn" type="xsd:string" />

  </message>

  <message name="GetBookPriceResponse">

    <part name="price" type="xsd:float" />

  </message>

  <message name="InvalidArgumentFault">

    <part name="error_message" element="mh:InvalidIsbnFaultDetail" />

  </message>

 

声明错误消息:

错误使用的消息定义仅仅能採用Document/Literal编码样式

上面声明了匿名类型,InvalidIsbnFaultDetail不须要type类型,complexType中也不包含name属性,具体见Web Service XML相关文档。

4.2 portType元素
             PortType元素定义了Web服务的抽象接口,它能够由一个或者多个operation元素,每一个operation元素定义了一个RPC样式或者文档样式的Web服务方法。

       4.3 operation元素
            Operation元素要用一个或者多个messages消息来定义它的输入、输出以及错误。


<message name="GetBulkBookPriceRequest">

  <part name="isbn" type="xsd:string"/>

  <part name="quantity" type="xsd:int"/>

</message>

<message name="GetBulkBookPriceResponse">

  <part name="prices" type="mh:prices" />

</message>

<message name="InvalidArgumentFault">

    <part name="error_message" element="mh:InvalidIsbnFaultDetail" />

  </message>

<portType name="GetBulkBookPrice" >

  <operation name="getBulkBookPrice" parameterOrder="isbn quantity">

     <input name="request" message="mh:GetBulkBookPriceRequest"/>

     <output name="prices" message="mh:GetBulkBookPriceResponse"/>

<fault name="InvalidArgumentFault" message="mh:InvalidArgumentFault"/>

  </operation>

</portType>

Input表示传递到Web服务的有效负载;output表示返回给客户的有效负载;能够不包含,也能够包含一个或者多个fault错误消息。

parameterOrder定义了input和output消息採用的正确的顺序

使用parameterOrder的时候,必须包含全部输入參数部分;而且仅仅包含不是返回类型的输出部分,假设output仅仅有一个part(上例),会假设返回值,所以不包含在parameterOrder中

假设parameterOrder列出output中的part部分,那么这个将被作为OUT參数,假设input元素和output元素使用同样的名称声明了一个部分的时候,此部分为INOUT參数


4.4 WSDL消息交换模式(MEP)
              Messaging Exchange Patterns(MEP)
              Web服务中使用了四种消息交换模式,即请求/响应、单向、通知以及恳求/响应模式。大多数基于WSDL的web服务使用请求/响应和单向两种模式。
              WSDL通过operation元素的input/output来定义使用那种模式,假设有input+output+可选的fault參数,那就使用请求/响应模式;假设仅仅使用input,那就使用单向模式。
              在通知模式中:Web服务将消息发送给客户,但不等待回复;一般客户通过注冊来接收通知;在恳求/响应模式中相似通知模式,唯一的差别要期待客户对Web服务的响应。

五、WSDL实现:binding元素
        Binding元素将一个抽象的portType映射到一组详细的协议(SOAP或者HTTP)、消息传递样式(RPC或者document)以及编码样式(literal或者SOAP encoding)。
        Binding的相似于将接口或者函数的调用绑定到某种协议上:比如CORBA、COM或者RPC的方式,这里使用SOAP协议。

        5.1 soapbind:binding元素


<binding name="BookPrice_Binding" type="mh:BookQuote">

  <soapbind:binding style="rpc"

   transport="http://schemas.xmlsoap.org/soap/http"/>

  <operation name="getBookPrice">

soapbind:binding元素指定了用于传输SOAP消息的Internet协议以及operation缺省的消息类型(RPC还是文档类型)

http://schemas.xmlsoap.org/soap/http表示採用的是HTTP的传输方式,当然也能够用HTTPS,用户详细使用HTTP还是HTTPS取决于Port元素中定义的location属性声明中的模式。

上面的rpc表示缺省状态下:operation将採用RPC的方式传递消息负载。

         5.2 soapbind:operation元素

<operation name="getBookPrice">

    <soapbind:operation style="rpc"

     soapAction=

     "http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice"/>

POST 1ed/BookQuote HTTP/1.1

Host: www.Monson-Haefel.com

Content-Type: text/xml; charset="utf-8"

Content-Length: nnnn

SOAPAction="http://www.Monson-Haefel.com/jwsbook/BookQuote/GetBookPrice"

soapbind:operation元素指定了消息传递样式(RPC或者document),而且指定了SOAPAction字段的值。

上面的样例显示在HTTP消息中的SOAPAction中相应的值

       5.3 soapbind:body元素

<operation name="getBookPrice">

<soapbind:operation style="rpc"/>

<input>

          <soapbind:body use="literal"

          namespace="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

       </input>

       <output>

          <soapbind:body use="literal"

          namespace="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

       </output>

<operation name="submit">

  <soapbind:operation style="document"/>

      <input>

        <soapbind:body use="literal" />

      </input>

      <output>

        <soapbind:body use="literal" />

      </output>

</operation>

soapbind:body元素有四个属性use、namespace、part和encodingStyle

对于WS-I use的属性值必须是literal,意味这不是用编码的方式,所以永远不会用到encodingStyle属性

在RPC样式中,必须用一个有效的URI指定的namespace属性。此URI能够于WSDL文档的targetNampspce同样;而在document样式中不能使用namespace,XML文档样式的命名空间派生于它的XML文档

          5.4 soapbind:fault元素

<fault name="InvalidArgumentFault">

     <soapbind:fault name="InvalidArgumentFault" use="literal" />

</fault>

<portType name="BookQuote">

  <operation name="getBookPrice">

     <input name="isbn" message="mh:GetBookPriceRequest"/>

     <output name="price" message="mh:GetBookPriceResponse"/>

     <fault name="InvalidArgumentFault" message="mh:InvalidArgumentFault"/>

  </operation>

</portType>

soapbind:fault元素和fault元素包括一个强制性的name属性,表示要引用声明于相应portType中的专有错误消息

          5.5 soapbind:header元素

<types>

<xsd:schema 

targetNamespace="http://www.Monson-Haefel.com/jwsbook/BookQuote"

     xmlns="http://www.w3.org/2001/XMLSchema">

        <xsd:element name="message-id" type="string" />

    </xsd:schema>

</types>

 

<!-- message elements describe the input and output parameters -->

  <message name="Headers">

    <part name="message-id" element="mh:message-id" />

  </message>

 

<operation name="getBookPrice">

  <input>

     <soapbind:header message="mh:Headers" part="message-id" use="literal" />

     <soapbind:body use="literal"

           namespace="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

        </input>

WSDL在绑定的input元素、output元素中利用soapbind:header元素显式指定了一个SOAP头文件

        5.6 soapbind:headerfault元素  
<!-- message elements describe the input and output parameters -->

  <message name="HeaderFault">

    <part name="faultDetail" element="mh:detailMessage" />

  </message>

 

<input>

      <soapbind:header message="mh:Header" use="literal">

         <soapbind:headerfault message="mh:Headers" use="literal" />

      </soapbind:header>

      <soapbind:body use="literal"

           namespace="http://www.Monson-Haefel.com/jwsbook/BookQuote" />

   </input>

soapbind:headerfault元素表述了Header专用的错误消息,假设有一个响应消息,必须在消息的Header元素中返回各种header的专用错误。

SOAP没有就怎样提供Header错误方面给出具体说明,仅仅是要求必须在Header元素中包括detail元素。有些SOAP工具箱将SOAP的fault放在header元素中。

六、WSDL实现:Service和Port元素

<service name="BookPriceService">

  <port name="BookPrice_Port" binding="mh:BookPrice_Binding">

    <soapbind:address location=

     "http://www.Monson-Haefel.com/jwsbook/BookQuote" />

  </port>

  <port name="BookPrice_Failover_Port" binding="mh:BookPrice_Binding">

    <soapbind:address location=

     "http://www.monson-haefel.org/jwsbook/BookPrice" />

  </port>

  <port name="SubmitPurchaseOrder_Port"

   binding="mh:SubmitPurchaseOrder_Binding">

    <soapbind:address location=

     "https://www.monson-haefel.org/jwsbook/po" />

  </port>

</service>

Service元素包括一个或者多个Port元素

每个Port元素相应一个不同的Web服务,port将一个URL赋予一个特定的binding,通过location实现

能够使两个或者多个port元素将不同的URL赋给同样的binding,比如负载平衡和容错的时候,使用这样的方法。

soapbind:address:将Internet地址通过location属性赋予一个SOAP绑定。

WebService 之 WSDL文件 解说的更多相关文章

  1. webservice 的wsdl文件生成客户端java类

    提供两个方法: 第一个: 发布webservice项目后, 地址栏地址  http://localhost:8888/lxitedu.webservice.cxf-ch2/services/userS ...

  2. So easy Webservice 5.WSDL 文件说明

    WSDL – WebService Description Language – Web服务描述语言 通过XML形式说明服务在什么地方-地址. 通过XML形式说明服务提供什么样的方法 – 如何调用. ...

  3. webservice 的wsdl文件详解

    转 WSDL(Web Service Description Language)Web服务描述语言 ################################################## ...

  4. WebService 之 WSDL文件 讲解

    原文地址:http://blog.csdn.net/tropica/archive/2008/11/02/3203892.aspx 恩,我想说的是,是不是经常有人在开发的时候,特别是和第三方有接口的时 ...

  5. C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用

    1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL.这个比较常见也很简单 即有完整的webservice文件目录如下图 ...

  6. 根据wsdl文件生成webservice 的.cs文件 及 生成dll C#调用

               Visual Studio 2013->Visual Studio Tools->VS2013 开发人员命令提示 命令行输入 wsdl E:\WS.wsdl /out ...

  7. webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成

    首先到CXF官网及spring官网下载相关jar架包,这个不多说.webservice是干嘛用的也不多说. 入门例子 模拟新增一个用户,并返回新增结果,成功还是失败. 大概的目录如上,很简单. Res ...

  8. eclipse根据.wsdl文件自动生成webservice的调用客户端

    1.工具:eclipse3.3或者是带有webservice插件的eclipse 2. 首先用浏览器访问webservice的站点,接着保存打开的页面,后缀为.wsdl. 3.把保存好的文件拷入ecl ...

  9. C#根据WSDL文件生成WebService服务端代码

    转自:http://www.cnblogs.com/liyi93/archive/2012/01/30/2332320.html 虽然现在已经进入了.NET FrameWork 4.0的时代,WebS ...

随机推荐

  1. img 的 align 属性

    AbsBottom 图像的下边缘与同一行中最大元素的下边缘对齐. AbsMiddle 图像的中间与同一行中最大元素的中间对齐. Baseline 图像的下边缘与第一行文本的下边缘对齐. Bottom ...

  2. 代理丶通知丶KVO之间区别和各自优势

    文/OyeOnoOmg(简书作者)原文链接:http://www.jianshu.com/p/75d3fd218a23著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 今天在网上看到一个 ...

  3. Codeforces Round #315 (Div. 2B) 569B Inventory 贪心

    题目:Click here 题意:给你n,然后n个数,n个数中可能重复,可能不是1到n中的数.然后你用最少的改变数,让这个序列包含1到n所有数,并输出最后的序列. 分析:贪心. #include &l ...

  4. VS Code开发调试ASP.NET Core 1.0

    VS Code开发调试ASP.NET Core 1.0 使用VS Code开发调试ASP.NET Core 1.0,微软在今天凌晨发布了.NET Core 1.0,ASP.NET Core 1.0 与 ...

  5. Hibernate执行sql语句

    Hibernate执行sql语句:BasicServiceImpl basicServiceImpl = new BasicServiceImpl();String hql = "selec ...

  6. Visual Studio 2015编译安装配置QT5.5.1(含QTWEBKIT)

    尽管QT5.5.1和VisualStudio 2015都已经发布很久了,但是QT项目组视乎不会为QT5.5.1专门发布预编译的QT5.5.1 for windows(2015)版本的,也不会专门发布V ...

  7. 引用 模块编译Makefile模板

    本文转载自geyingzhen<模块编译Makefile模板>   引用 geyingzhen 的 模块编译Makefile模板 ifneq ($(KERNELRELEASE), ) // ...

  8. vector的内存释放

    转自:http://www.cnblogs.com/summerRQ/articles/2407974.html vector : C++ STL中的顺序容器,封装数组 1. vector容器的内存自 ...

  9. 动态规划之一ones

    n给一个整数n,要你找一个值为n的表达式,这个表达式只有1 + * ( ) 够成.并且1不能连续,比如11+1就不合法. n输入n,(1<=n<=10000) n输出最少需要多少个1才能构 ...

  10. 用Python做2048游戏 网易云课堂配套实验课。通过GUI来体验编程的乐趣。

    第1节 认识wxpython 第2节 画几个形状 第3节 再做个计算器 第4节 最后实现个2048游戏 实验1-认识wxpython 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiy ...