信息化 频道

使用WSDL 2.0描述REST Web服务

下面几个部分将更详细地介绍本部分中讨论的所有WSDL元素。

  处理图书列表服务

  提醒一下,图书列表服务的URL为 http://www.bookstore.com/books/。为了处理该服务,您将使用WSDL service元素,此元素至少需要一个endpoint子元素。endpoint元素的address属性用于指定该服务的URL,如清单 4 所示。endpoint元素还用于将某个绑定与具有 binding 属性的服务关联起来。service元素反过来又将某个接口与具有interface属性的服务关联起来。您将在以下几个部分中创建接口和绑定,因此现在可以将这些属性值保留空白。

  图书列表服务的WSDL 2.0文档还需要一个目标命名空间,因此定义命名空间http://www.bookstore.org/booklist/wsdl

  清单 4. 图书列表服务定义

  以下是引用片段:
  <wsdl:description xmlns:wsdl="http://www.w3.org/ns/wsdl"
  targetNamespace="http://www.bookstore.org/booklist/wsdl"
  xmlns:tns="http://www.bookstore.org/booklist/wsdl">
  <wsdl:service name="BookList" interface="">
  <wsdl:documentation>
  The bookstore's book list service.
  </wsdl:documentation>
  <wsdl:endpoint name="BookListHTTPEndpoint"
  binding=""
  address="http://www.bookstore.com/books/">
  </wsdl:endpoint>
  </wsdl:service>
  </wsdl:description> 


  与图书列表服务进行HTTP通信

  图书列表服务的绑定定义需要指定该服务使用HTTP来进行通信。为此,可以为binding元素的type属性指定值http://www.w3.org/ns/wsdl/http

  该绑定还可以选择引用某个接口。将interface属性保留空白;您将在下一个部分中创建该属性。如果某个接口与该绑定相关联,binding 可以选择声明一个子operation元素,用于镜像接口的operation元素。您需要创建一个存根operation元素,并在创建接口以后填充此引用。

  存在四个HTTP通信谓词: GET 、PUT 、POST 、DELETE

  图书列表服务是读取请求,因此使用HTTP GET进行通信。使用WSDL 2.0 HTTP命名空间中的HTTP method属性,在operation元素上设置GET谓词。要使用此属性,您首先需要在Description元素上声明命名空间http://www.w3.org/ns/wsdl/http

  清单 5 显示了图书列表服务的HTTP绑定声明。现在既然已经声明了绑定,您可以将endpoint元素的绑定引用更新为引用 tns:BookListHTTPBinding。

  清单 5. 图书列表绑定定义

  以下是引用片段:
  <wsdl:description xmlns:wsdl="http://www.w3.org/ns/wsdl"
  targetNamespace="http://www.bookstore.org/booklist/wsdl"
  xmlns:tns="http://www.bookstore.org/booklist/wsdl"
  xmlns:whttp="http://www.w3.org/ns/wsdl/http">
  <wsdl:binding name="BookListHTTPBinding"
  type="http://www.w3.org/ns/wsdl/http"
  interface="">
  <wsdl:documentation>
  The RESTful HTTP binding for the book list service.
  </wsdl:documentation>
  <wsdl:operation ref="" whttp:method="GET"/>
  </wsdl:binding>
  <wsdl:service name="BookList" interface="">
  <wsdl:documentation>
  The bookstore's book list service.
  </wsdl:documentation>
  <wsdl:endpoint name="BookListHTTPEndpoint"
  binding="tns:BookListHTTPBinding"
  address="http://www.bookstore.com/books/">
  </wsdl:endpoint>
  </wsdl:service>
  </wsdl:description>


  定义图书列表服务操作

  到目前为止,您已经了解如何处理图书列表Web服务并与之通信。接下来您将指定图书列表服务操作,该操作描述图书列表服务的功能。

  interface元素及其子operation元素用于定义服务的操作。在该图书列表服务的情况下,您将定义单个操作getBookList,此操作使用图书列表来响应请求。

  下一步,指定operation元素的三个属性:

  pattern:用于指定该操作的消息交换模式(message exchange pattern,MEP)。MEP定义操作中的消息顺序及其方向。在此例中,请指定值 http://www.w3.org/ns/wsdl/in-out,以指示该服务接收一个输入消息——图书列表请求——并发送一个输出消息——图书列表。为了支持此MEP,请指定operation元素的input和output子元素。这些元素用于引用定义消息结构的XML模式元素,下一部分将创建那些消息结构。

  style:用于指定有关某个操作的附加信息。请指定值http://www.w3.org/ns/wsdl/style/iri,从而对input元素内容施加限制,例如要求它仅使用XML模式元素。

  wsdlx:safe:在WSDL扩展命名空间中,此属性声明该操作是等幂的。这种类型的操作不修改资源,因此可以调用多次并获得相同的结果。为了利用此元素,可以在description元素上声明WSDL扩展命名空间 http://www.w3.org/ns/wsdl-extensions

  您可以在“WSDL 2.0 Part 2: Adjuncts”(请参阅参考资料以获得链接)中找到预定义的 MEP样式和afe属性定义。

  在清单 6 中查看图书列表服务的接口声明。既然现在已经声明了接口和操作,您可以更新service和binding元素的接口引用和绑定operation元素的接口操作引用。

  清单 6. 图书列表接口定义

  以下是引用片段:
  <wsdl:description xmlns:wsdl="http://www.w3.org/ns/wsdl"
  targetNamespace="http://www.bookstore.org/booklist/wsdl"
  xmlns:tns="http://www.bookstore.org/booklist/wsdl"
  xmlns:whttp="http://www.w3.org/ns/wsdl/http"
  xmlns:wsdlx="http://www.w3.org/ns/wsdl-extensions">
  <wsdl:interface name="BookListInterface">
  <wsdl:operation name="getBookList"
  pattern="http://www.w3.org/ns/wsdl/in-out"
  style="http://www.w3.org/ns/wsdl/style/iri"
  wsdlx:safe="true">
  <wsdl:documentation>
  This operation returns a list of books.
  </wsdl:documentation>
  <wsdl:input element=""/>
  <wsdl:output element=""/>
  </wsdl:operation>
  </wsdl:interface>
  <wsdl:binding name="BookListHTTPBinding"
  type="http://www.w3.org/ns/wsdl/http"
  interface="tns:BookListInterface">
  <wsdl:documentation>
  The RESTful HTTP binding for the book list service.
  </wsdl:documentation>
  <wsdl:operation ref="tns:getBookList" whttp:method="GET"/>
  </wsdl:binding>
  <wsdl:service name="BookList" interface="tns:BookListInterface">
  <wsdl:documentation>
  The bookstore's book list service.
  </wsdl:documentation>
  <wsdl:endpoint name="BookListHTTPEndpoint"
  binding="tns:BookListHTTPBinding"
  address="http://www.bookstore.com/books/">
  </wsdl:endpoint>
  </wsdl:service>
  </wsdl:description>


  定义图书列表服务操作消息

  图书列表Web服务具有两个消息:一个输入消息和一个输出消息。您需要描述特定的消息结构,以便客户端知道要向服务发送什么消息,以及预期服务会返回什么消息。

  WSDL 2.0支持使用多种类型系统来描述消息内容,但是目前仅使用了XML模式。本部分不介绍XML模式的详细信息。许多应用程序中都使用了XML模式,例如WSDL 1.1,并且存在许多关于它的优秀文章。本部分重点介绍如何将XML模式用于图书列表REST Web服务,以及如何使用WSDL 2.0定义的附加属性来标注模式属性。

  要为图书列表REST Web服务创建两个消息,您需要创建两个全局元素:

  getBookList表示输入消息。其中包含元素序列,包括服务上允许的每个查询参数,即作者、标题、出版商、主题和语言。getBookList元素的内容仅限于元素,因为您为接口操作指定了 IRI 样式。

  bookList表示输出消息。其中包含book元素的序列。每个book元素包含title和url属性。title属性应该是不言而喻的。url属性是指向图书详细信息REST Web服务的链接,该服务返回特定图书的详细信息。

  您的url属性定义包括两个来自WSDL扩展命名空间的属性。属 wsdlx:interface和wsdlx:binding标识该服务的特定WSDL 2.0接口和绑定。工具可以使用此语义信息来自动发现该服务。要利用这些属性,可以在schema元素上指定WSDL扩展命名空间。此外,还要在 WSDL 2.0 描述中包括图书详细信息服务的命名空间,以引用该服务的接口和绑定。

  图书列表服务的XML模式如清单 7 所示。您可以在下载部分获取图书详细信息服务的描述。

  清单 7. 图书列表服务的XML模式

  以下是引用片段:
  <schema xmlns="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.bookstore.org/booklist/xsd"
  xmlns:tns="http://www.bookstore.org/booklist/xsd"
  xmlns:booksvc="http://www.bookstore.org/book/wsdl"
  xmlns:wsdlx="http://www.w3.org/ns/wsdl-extensions"
  elementFormDefault="qualified">

  <element name="getBookList" type="tns:getBookListType">
  <annotation>
  <documentation>
  The request element for the book list service.
  </documentation>
  </annotation>
  </element>

  <element name="bookList" type="tns:bookListType">
  <annotation>
  <documentation>
  The response element for the book list service.
  </documentation>
  </annotation>
  </element>

  <complexType name="getBookListType">
  <sequence>
  <element name="author" type="string" minOccurs="0" maxOccurs="unbounded"/>
  <element name="title" type="string" minOccurs="0" maxOccurs="1"/>
  <element name="publisher" type="string" minOccurs="0" maxOccurs="1"/>
  <element name="subject" type="string" minOccurs="0" maxOccurs="1"/>
  <element name="language" type="string" minOccurs="0" maxOccurs="unbounded"/>
  </sequence>
  </complexType>
  <complexType name="bookListType">
  <sequence>
  <element name="book" type="tns:bookType" minOccurs="0" maxOccurs="unbounded"/>
  </sequence>
  </complexType>
  <complexType name="bookType">
  <attribute name="title" type="string"/>
  <attribute name="url" type="anyURI" 
  wsdlx:interface="booksvc:BookInterface"
  wsdlx:binding="booksvc:BookHTTPBinding"/>
  </complexType>
  </schema>


  要引用XML模式中声明的输入和输出元素,您必须将该模式导入您的WSDL文档。要导入某个模式,您可以在types部分使用import元素,如清单 8 所示。您还需要在接口操作的input和output元素中添加对getBookList和bookList元素的引用,并将XML模式(图书列表模式)的命名空间声明添加到description元素。

  清单 8 显示了图书列表REST Web服务的完整WSDL 2.0描述。可以在下载部分中获取此服务和图书详细信息REST Web服务的描述。

  清单 8. 图书列表REST Web服务的WSDL 2.0描述

  以下是引用片段:
  <wsdl:description xmlns:wsdl="http://www.w3.org/ns/wsdl"
  targetNamespace="http://www.bookstore.org/booklist/wsdl"
  xmlns:tns="http://www.bookstore.org/booklist/wsdl"
  xmlns:whttp="http://www.w3.org/ns/wsdl/http"
  xmlns:wsdlx="http://www.w3.org/ns/wsdl-extensions"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:msg="http://www.bookstore.org/booklist/xsd">
  <wsdl:documentation>
  This is a WSDL 2.0 description of a sample bookstore service
  listing for obtaining book information.
  </wsdl:documentation>
  <wsdl:types>
  <xs:import namespace="http://www.bookstore.org/booklist/xsd"
  schemaLocation="booklist.xsd"/>
  </wsdl:types>
  <wsdl:interface name="BookListInterface">
  <wsdl:operation name="getBookList"
  pattern="http://www.w3.org/ns/wsdl/in-out"
  style="http://www.w3.org/ns/wsdl/style/iri
  wsdlx:safe="true">
  <wsdl:documentation>
  This operation returns a list of books.
  </wsdl:documentation>
  <wsdl:input element="msg:getBookList"/>
  <wsdl:output element="msg:bookList"/>
  </wsdl:operation>
  </wsdl:interface>
  <wsdl:binding name="BookListHTTPBinding"
  type="http://www.w3.org/ns/wsdl/http"
  interface="tns:BookListInterface">
  <wsdl:documentation>
  The RESTful HTTP binding for the book list service.
  </wsdl:documentation>
  <wsdl:operation ref="tns:getBookList" whttp:method="GET"/>
  </wsdl:binding>
  <wsdl:service name="BookList" interface="tns:BookListInterface">
  <wsdl:documentation>
  The bookstore's book list service.
  </wsdl:documentation>
  <wsdl:endpoint name="BookListHTTPEndpoint"
  binding="tns:BookListHTTPBinding"
  address="http://www.bookstore.com/books/">
  </wsdl:endpoint>
  </wsdl:service>
  </wsdl:description>


  总结

  在本文中,您了解REST以及WSDL 2.0 REST Web服务如何使用HTTP和XML来进行通信。RESTful应用程序以资源为中心而不是以操作为中心。以正式的方式描述REST Web服务的价值是使用该描述作为客户端与服务提供者之间的正式契约,并支持工具。WSDL 2.0支持REST Web服务的描述。您逐步地完成了使用WSDL 2.0 和XML模式来描述图书列表REST Web服务的过程。

0
相关文章