WSDL 守则解释(转)

WSDL 规则解释(转)

转自 http://www.blogjava.net/baoyaer/articles/116413.html

 

WSDL文档可以分为两部分。顶部分由抽象定义组成 ,而底部分则由具体描述组成。抽象部分以独立于平台和语言的方式定义SOAP消息,它们并不包含任何随机器或语言而变的元素。这就定义了一系列服务,截然不同的网站都可以实现。随网站而异的东西如序列化便归入底部分,因为它包含具体的定义。
l 抽象定义
Types - 独立与机器和语言的类型定义

Messages - 包括函数参数(输入与输出分开)或文档描述

PortTypes - 引用消息部分中消息定义来描述函数签名(操作名、输入参数、输出参数)

2 具体定义
Bindings
PortTypes - 部分的每一操作在此绑定实现 (one-way|reuqest-response|solicit-response|notification)

Services - 确定每一绑定的端口地址

 

<definitions>
  <types> ... </types>
  <message> ... </message>
  <portType> ... </portType>
  <binding> ... </binding>
  <service> ... </serivce>
</definitions>
 


下面的图中,箭头连接符代表文档不同栏 之间的关系。点和箭头代表了引用或使用关系。双箭头代表"修改"关系。3-D的箭头代表了包含关系。这样,各Messages栏使用Types栏的定 义,PortTypes栏使用Messages栏的定义;Bindings栏引用了PortTypes栏,Services栏引用Bindings 栏,PortTypes和Bindings栏包含了operation元素,而Services栏包含了port元素。PortTypes栏里的 operation元素由Bindings栏里的operation元素进一步修改或描述。
WSDL 守则解释(转)



首先看看一段简单的JAVA代码,我们用它作为WSDL的服务实现代码:

public class Test{
  public String echo(String u){
    return "Hello " + u;
  }
}

看看它导出的文件:

第一行申明该文档是XML。尽管这并不是必需的,但它有助于XML解析器决定是否解析 WSDL文件或只是报错。第二行是WSDL文档的根元素:<definitions>。一些属性附属于根元素,就 像<schema>子元素对于<types>元素。

<?xml version="1.0" encoding="UTF-8" ?> 
<wsdl:definitions targetNamespace="http://localhost/axis/Test2.jws"
                                      xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
                                      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                                      xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
                                      xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
                                      xmlns:apachesoap="http://xml.apache.org/xml-soap" 
                                      xmlns:intf="http://localhost/axis/Test2.jws" 
                                      xmlns:impl="http://localhost/axis/Test2.jws">
<!-- 
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
--> 

 

定义好操作(或方法)以后,现在需要指定将向它们发送和从它们返回的参数。在 WSDL 术语中,所有参数称为“消息”。认为您是在递送消息而结果得到返回的消息是有用的。方法调用是这样一种操作:它准备返回“消息”来响应进入的消息。

 


<message>元素包含了Messages栏。如果我们把操作看作函数,<message>元素定义了那个 函数的参数。<message>元素中的每个<part>子元素都和某个参数相符。输入参数在<message>元 素中定义,与输出参数相隔离--输出参数有自己的<message>元素。兼作输入、输出的参数在输入输出的<message> 元素中有它们相应的<part>元素。输出<message>元素以"Response"结尾,就像以前所用 的"fooResponse"。每个<part>元素都有名字和类型属性,就像函数的参数有参数名和参数类型。

<wsdl:message name="echoResponse">   //返回的消息
  <wsdl:part name="echoReturn" type="xsd:string" /> 
</wsdl:message>
<wsdl:message name="echoRequest">  //请求的消息
  <wsdl:part name="u" type="xsd:string" /> 
</wsdl:message>

 

<definitions> 元素包含一个或多个 <portType> 元素,实际上,每个元素都是您希望表示的一系列 operation 。或者,您也可以将单个 portType 元素看作是将各种方法组成类的一个逻辑分组。例如,如果您的供应链管理解决方案需要在客户和供应商之间进行交互,您最可能做的是分别定义与他们交互的功能 性;也就是说,您将为用户和供应商各定义一个 portType。应该将每个 portType 称为 服务 ,因此整个 WSDL 文件将成为一个服务集合。

 

<wsdl:portType name="Test2">   //一个portType可以看成一个类
   <wsdl:operation name="echo" parameterOrder="u">   //一个operation就是一个方法
     <wsdl:input name="echoRequest" message="impl:echoRequest" />   //输入消息
     <wsdl:output name="echoResponse" message="impl:echoResponse" />  //返回消息
   </wsdl:operation>
</wsdl:portTyp>

 

 

消息传递和传输:
我以一种抽象方式定义了操作和消息,而不考虑实现的细节。实际上,WSDL 的任务是定义或描述 Web 服务,然后提供一个对外部框架的引用来定义 WSDL 用户将如何实现这些服务。可以将这个框架当作 WSDL 抽象定义和它们的实现之间的“绑定( binding )”。

当前,最流行的绑定( binding )技术是使用简单对象访问协议 (SOAP)。WSDL 将指定能够访问 Web 服务实际实现的 SOAP 服务器,并且从那时起 SOAP 的整个任务就是将用户从 WSDL 文件带到它的实现

 

 

WSDL 编写的第三个步骤是描述将 SOAP 与 WSDL 文件绑定到一起的过程

您将把 <binding> 元素包括到 <definitions> 元素内。这个 binding 元素应该有 nametype 属性。 name 将标识这个绑定而 type 将标识您希望与这个绑定相关联的 portType(一组操作)。

<wsdl:binding name="Test2SoapBinding" type="impl:Test2">...</wsdl:binding>

<wsdlsoap:binding> 元素。该元素的用途是声明将把 SOAP 作为绑定和传输服务使用

<wsdlsoap:binding> 元素有两个属性:style 和 transport 。style 是一个可选属性(rpc|document),它描述该绑定内操作的性质。transport 属性指定 HTTP 作为该绑定将使用的级别较低的传输服务。SOAP 客户机将从 WSDL 文件中读取 SOAP 结构并与另一端的 SOAP 服务器协调.

<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 

WSDL <operation> 元素,分别表示具体的操作。每个 <operation> 元素提供各自操作的绑定细节。因此,我提供了另一个 extensibility 元素,即 <wsdlsoap:operation/> (仍然是一个空元素,与它发生的那个操作相关)。该 <soap:operation/> 元素有一个 soapAction 属性,SOAP 客户机将使用该属性创建 SOAP 请求。 

 

//下面将WSDL描述与具体实现进行绑定,这里采用SOAP方式
<wsdl:binding name="Test2SoapBinding" type="impl:Test2">
  <wsdl:operation name="echo">
    <wsdlsoap:operation soapAction="" /> 
      <wsdl:input name="echoRequest">
         <wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" /> 
      </wsdl:input>
      <wsdl:output name="echoResponse">
         <wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/axis/Test2.jws" /> 
      </wsdl:output>
    </wsdl:operation>
</wsdl:binding>

 

//下面将发布的服务与前面的SOAP绑定进行关联
<wsdl:service name="Test2Service">
  <wsdl:port name="Test2" binding="impl:Test2SoapBinding">
    <wsdlsoap:address location="http://localhost/axis/Test2.jws" /> 
  </wsdl:port>
</wsdl:service>
 

每个namespace属性都声明了一个缩略语,用在文档中。例如"xmlns:xsd"就为 http://www.w3.org/2001/XMLSchema定义了一个缩略语(xsd)。这就允许对该namespace的引用只需简单的在名字 前加上前缀就可以了,如:"xsd:int"中的"xsd"就是合法的类型名。普通范围规则可运用于缩略前缀。也就是说,前缀所定义的元素只在元素中有 效。

Namespace派什么用?namespace的作用是要避免命名冲突。如果我建立一项Web Service,其中的WSDL文件包含一个名为"foo"的元素,而你想要使用我的服务与另一项服务连接作为补充,这样的话另一项服务的WSDL文件就 不能包含名为"foo"的元素。两个服务器程序只有在它们在两个事例中表示完全相同的东西时,才可以取相同的名字。如果有了表示区别的 namespace,我的网络服务里的"foo"就可以表示完全不同于另一个网络服务里"foo"的含义。在你的客户端里,你只要加以限制就可以引用我 的"foo"。

见下例:http://www.infotects.com/fooService#foo 就是完全限制的名字,相当于"carlos:foo",如果我声明了carlos作为http://www.infotects.com /fooService的快捷方式。请注意namespace中的URL是用来确定它们的唯一性的,同时也便于定位。URL所指向的地方不必是实际存在的 网络地址,也可以使用GUID来代替或补充URL。例如,GUID"335DB901-D44A-11D4-A96E-0080AD76435D"就是一 个合法的namespace指派。

targetNamespace属性声明了一个namespace,元素中所有的声明的名字都列于其内。在WSDL示例中,<definitions>的targetNamespace 是http://tempuri.org/wsdl 。这意味着所有在WSDL文档中声明的名字都属于这个namespace。<schema>元素有自己的targetNamespace属性,其值为 http://tempuri.org/xsd ,在<schma>元素中定义的所有名字都属于这个namespace而不是main的target namespace。

 

<schema>元素的以下这行声明了默认的namespace。Schema中所有有效的名字都属于这个namespace。

 

 

附一个比较完整的例子,以供参考学习:

 

SOAP request

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <m:GetEndorsingBoarder xmlns:m="http://namespaces.snowboard-info.com">
      <manufacturer>K2</manufacturer>
      <model>Fatbob</model>
    </m:GetEndorsingBoarder>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

SOAP response

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <m:GetEndorsingBoarderResponse xmlns:m="http://namespaces.snowboard-info.com">
      <endorsingBoarder>Chris Englesmann</endorsingBoarder>
    </m:GetEndorsingBoarderResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
 

WSDL description

<?xml version="1.0"?>

<!-- root element wsdl:definitions defines set of related services -->
<wsdl:definitions name="EndorsementSearch"
  targetNamespace="http://namespaces.snowboard-info.com"
  xmlns:es="http://www.snowboard-info.com/EndorsementSearch.wsdl"
  xmlns:esxsd="http://schemas.snowboard-info.com/EndorsementSearch.xsd"
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

  <!-- wsdl:types encapsulates schema definitions of communication types; here using xsd -->
  <wsdl:types>

    <!-- all type declarations are in a chunk of xsd -->
    <xsd:schema targetNamespace="http://namespaces.snowboard-info.com"
      xmlns:xsd="http://www.w3.org/1999/XMLSchema">

      <!-- xsd definition: GetEndorsingBoarder [manufacturer string, model string] -->
      <xsd:element name="GetEndorsingBoarder">
	<xsd:complexType>
	  <xsd:sequence>
	    <xsd:element name="manufacturer" type="string"/>
            <xsd:element name="model" type="string"/>
	  </xsd:sequence>
	</xsd:complexType>
      </xsd:element>

      <!-- xsd definition: GetEndorsingBoarderResponse [... endorsingBoarder string ...] -->
      <xsd:element name="GetEndorsingBoarderResponse">
	<xsd:complexType>
	  <xsd:all>
	    <xsd:element name="endorsingBoarder" type="string"/>
	  </xsd:all>
	</xsd:complexType>
      </xsd:element>

    <!-- xsd definition: GetEndorsingBoarderFault [... errorMessage string ...] -->
    <xsd:element name="GetEndorsingBoarderFault">
	<xsd:complexType>
	  <xsd:all>
	    <xsd:element name="errorMessage" type="string"/>
	  </xsd:all>
	</xsd:complexType>
      </xsd:element>

    </xsd:schema>
  </wsdl:types>

  <!-- wsdl:message elements describe potential transactions -->

  <!-- request GetEndorsingBoarderRequest is of type GetEndorsingBoarder -->
  <wsdl:message name="GetEndorsingBoarderRequest">
    <wsdl:part name="body" element="esxsd:GetEndorsingBoarder"/>
  </wsdl:message>

  <!-- response GetEndorsingBoarderResponse is of type GetEndorsingBoarderResponse -->
  <wsdl:message name="GetEndorsingBoarderResponse">
    <wsdl:part name="body" element="esxsd:GetEndorsingBoarderResponse"/>
  </wsdl:message>

  <!-- wsdl:portType describes messages in an operation -->
  <wsdl:portType name="GetEndorsingBoarderPortType">

    <!-- the value of wsdl:operation eludes me -->
    <wsdl:operation name="GetEndorsingBoarder">
      <wsdl:input message="es:GetEndorsingBoarderRequest"/>
      <wsdl:output message="es:GetEndorsingBoarderResponse"/>
      <wsdl:fault message="es:GetEndorsingBoarderFault"/>
    </wsdl:operation>
  </wsdl:portType>

  <!-- wsdl:binding states a serialization protocol for this service -->
  <wsdl:binding name="EndorsementSearchSoapBinding"
                type="es:GetEndorsingBoarderPortType">

    <!-- leverage off soap:binding document style @@@(no wsdl:foo pointing at the soap binding) -->
    <soap:binding style="document"
                  transport="http://schemas.xmlsoap.org/soap/http"/>

    <!-- semi-opaque container of network transport details classed by soap:binding above @@@ -->
    <wsdl:operation name="GetEndorsingBoarder">

    <!-- again bind to SOAP? @@@ -->
    <!--  if <wsdl:operation name="EndorsementSearch"> exists, then, soapAction seems can be ingored -->
    <soap:operation soapAction="http://www.snowboard-info.com/EndorsementSearch"/>

     <!-- furthur specify that the messages in the wsdl:operation "GetEndorsingBoarder" use SOAP? @@@ -->
     <wsdl:input>
        <soap:body use="literal"
		   namespace="http://schemas.snowboard-info.com/EndorsementSearch.xsd"/>
     </wsdl:input>
     <wsdl:output>
        <soap:body use="literal"
		   namespace="http://schemas.snowboard-info.com/EndorsementSearch.xsd"/>
     </wsdl:output>
     <wsdl:fault>
        <soap:body use="literal"
		   namespace="http://schemas.snowboard-info.com/EndorsementSearch.xsd"/>
     </wsdl:fault>
    </wsdl:operation>
  </wsdl:binding>

  <!-- wsdl:service names a new service "EndorsementSearchService" -->
  <wsdl:service name="EndorsementSearchService">
    <wsdl:documentation>snowboarding-info.com Endorsement Service</wsdl:documentation> 

    <!-- connect it to the binding "EndorsementSearchSoapBinding" above -->
    <wsdl:port name="GetEndorsingBoarderPort"
               binding="es:EndorsementSearchSoapBinding">

      <!-- give the binding an network address -->
      <soap:address location="http://www.snowboard-info.com/EndorsementSearch"/>
    </wsdl:port>
  </wsdl:service>

 </wsdl:definitions>
 

 

1 楼 darkjune 2011-08-31  
很详细,<wsdl:####>这个前缀如果ns里面没有定义wsdl,可以省略的吧?
2 楼 calvinlyc 2011-09-05  
darkjune 写道
很详细,<wsdl:####>这个前缀如果ns里面没有定义wsdl,可以省略的吧?

:) 不好意思,我没明白你的问题,如果是定义wsdl的话,
wsdl:types 
wsdl:message 
wsdl:portType 
wsdl:binding 
wsdl:service
这几个都基本上是必须的