描述与注册 发布Web服务(一)

日期: 2008-09-26 作者:柴晓路 来源:TechTarget中国 英文

  本文是架构Web服务的系列文章的第六篇,也是最后一篇,文本以前文为基础,在前文的应用实例的基础上,考察了发布Web服务界面的整个过程:XML Schema建模、WSDL发布和UDDI注册。通过本文,大家可以详细具体地了解各个XML和Web Service的系列规范在Web Service的发布时所起的左右,对Web Service技术也将有一个深入的理解。


  在前文中,我已经介绍过,Web服务是通过SOAP消息调用的,通过WSDL进行界面描述的,以及通过UDDI进行公共注册发布的。在前一篇文章中,我已经介绍了如何进行SOAP API的消息定义,那么在本文中,我将单把save_category提出来,看看在具体的实现上,应当如何对这个消息使用W3C XML Schema进行建模,如果使用WSDL将基于该消息调用的Web服务进行规范描述并交付调用者,以及如何将这个Web服务连同它的WSDL规范化描述文件一起发布到UDDI注册中心中去。希望大家能通过本文的实例讲解,在本系列的最后完整地了解Web服务的工作原理和相关技术规范的作用。


  本文所引用的资源主要包括两类,一类是Web服务的技术资源网站,包含了大量Web服务的技术信息,另一类是Web服务“stack”系列技术规范,他们是一个整体的技术体系,包括UDDI、SOAP、WSDL、XML Schema, XML等。本文的最后给出了这些资源的链接,有兴趣的读者可以通过这些 资源链接找到所需的内容。


  SOAP消息示例


  以下是一个save_category的调用例子,在例子中使用了SOAP HTTP Binding(使用的SOAP规范的版本是1.2),假设目标Web服务被部署在www.sagitta.com,而调用的Web服务的入口位置将是http://www.sagitta.com/catalog/。


  在这个消息中,将在一个现有的category中添加一个新的category和一个新的product。


  POST /catalog HTTP/1.1
  Content-Type: text/xml; charset=”utf-8″
  Content-Length: nnnn
  SOAPAction: “http://www.sagitta.com/catalog/”
  Host: www.sagitta.com
  <?xml version=”1.0″ encoding=”UTF-8″ ?>
  <env:Envelope >
    <env:Body>
      <save_category >
        <authInfo>5Az784kJceHCE982eB</authInfo>
        <category categoryKey=”cb4e17d1-6100-47f6-a532-cd9cbd30c073″
          parentCategoryKey=”ab4e3de1-7865-f2c1-b49a-beccbd21c072″>
          <name>Consumer Electronics</name>
          <description>Product Category for Consumer Electronics </description>
          <category categoryKey=”” parentCategoryKey=”cb4e17d1-6100-47f6-a532-cd9cbd30c073″>
            <name>SONY Consumer Electronics</name>
            <description>Sony’s Product Category for Consumer Electronics</description>
          </category>
          <product productKey=”” parentCategoryKey=” cb4e17d1-6100-47f6-a532-cd9cbd30c073″>
            <name>DSC-S75 Digital Camera</name>
            <description>Sony’s Brand-New Professional Digital Camera</description>
            <compliantSpecBag>
              <specification specificationKey=”Key[USB1.1]” />
            </compliantSpecBag>
            <featureBag>
              <feature>……</feature>
              <feature>……</feature>
            </featureBag>
            <parameterBag>
              ……
            </parameterBag>
          </product>
        </category>
      </save_category>
    </env:Body>
  </env:Envelope>
 
  该调用消息的返回消息可能是: HTTP/1.1 200 OK
  Content-Type: text/xml; charset=”utf-8″
  Content-Length: nnnn
  <?xml version=”1.0″ encoding=”UTF-8″ ?>
  <env:Envelope >
    <env:Body>
      <categoryList >
        <category categoryKey=”cb4e17d1-6100-47f6-a532-cd9cbd30c073″
        parentCategoryKey=”ab4e3de1-7865-f2c1-b49a-beccbd21c072″>
          <category categoryKey=”8933aa50-3aaf-11d5-80dc-002035229c64″
          parentCategoryKey=”cb4e17d1-6100-47f6-a532-cd9cbd30c073″ />
          <product productKey=”89307600-3aaf-11d5-80dc-002035229c64″
          parentCategoryKey=”cb4e17d1-6100-47f6-a532-cd9cbd30c073″ />
        </category>
      </categoryList>
    </env:Body>
  </env:Envelope>
 
  从中我们可以看到在save_category和categoryList两个元素后面都带了一个 encoding=”UTF-8″?>
  <xs:schema   elementFormDefault=”qualified” attributeFormDefault=”unqualified”>
 
  以上是XML Schema的文件头。


   <xs:complexType name=”compliantSpecBag”>
      <xs:sequence>
        <xs:element name=”specification” minOccurs=”0″ maxOccurs=”unbounded”>
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base=”xs:string”>
                <xs:attribute name=”specificationKey” type=”xs:string” use=”required”/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
 
  在这段Schema描述中,描述了compliantSpecBag这个元素类型,任何使用compliantSpecBag类型的元素可以包含的元素是specification,这个元素可以出现0次到无穷次(无限制,事实上不可能出现无穷次),而specification这个元素中包含一个属性specificationKey,该属性是必须的,同时类型为字符串(xs:string)。xs这个命名空间是W3C XML Schema 2001的命名空间(namespace)。


   <xs:complexType name=”featureBag”>
      <xs:sequence>
        <xs:element name=”feature” type=”xs:string” minOccurs=”0″ maxOccurs=”unbounded”/>
      </xs:sequence>
    </xs:complexType>
 
  在这里,描述了featureBag这个元素类型,任何使用featureBag类型的文档元素可以包含的子元素是feature,而feature元素能够出现0次到无穷次(无限制,事实上不可能出现无穷次)。该元素的内容的类型是字符串(xs:string)。


   <xs:complexType name=”parameter”>
      <xs:sequence>
        <xs:element name=”keyName” type=”xs:string”/>
        <xs:element name=”keyValue” type=”xs:string”/>
      </xs:sequence>
    </xs:complexType>
    <xs:complexType name=”parameterBag”>
      <xs:sequence>
        <xs:element name=”parameter” type=”parameter” minOccurs=”0″ maxOccurs=”unbounded”/>
      </xs:sequence>
    </xs:complexType>
 
  这里首先描述了parameter这个元素类型,任何使用parameter类型的文档元素可以包含的子元素有两个keyName和keyValue,它们都是必须出现的子元素,同时仅可出现一次。他们的类型也都是字符串(xs:string)。然后描述的是parameterBag元素类型,任何使用parameterBag类型的文档元素可以包含的子元素是parameter,而这个子元素的类型是先前定义的parameter(在XML Schema中,类型名和元素名的域空间是正交的,不需要考虑任何的名字重复问题),parameter元素出现的次数可以是从0次到无穷次。


   <xs:complexType name=”product”>
      <xs:sequence>
        <xs:element name=”name” type=”xs:string”/>
        <xs:element name=”description” type=”xs:string”/>
        <xs:element name=”compliantSpecBag” type=”compliantSpecBag”/>
        <xs:element name=”featureBag” type=”featureBag”/>
        <xs:element name=”parameterBag” type=”parameterBag”/>
      </xs:sequence>
      <xs:attribute name=”productKey” type=”xs:string” use=”required”/>
          <xs:attribute name=”parentCategoryKey” type=”xs:string” use=”required”/>
    </xs:complexType>
 
  这里描述了product这个元素类型,任何使用product类型的文档元素可以包含的子元素是name、description、compliantSpecBag、featureBag、parameterBag。name和description元素的类型都是xs:string。而compliantSpecBag、featureBag、parameterBag的类型大家通过查看这断XML Schema定义也可以很清楚地发现是引用了前面定义的这些类型定义。任何使用product类型的文档元素还有两个必须出现的属性productKey和parentCategoryKey,这是两个字符串(xs:string)类型的属性值,分别表示了自身元素的键值和父辈category的键值。


   <xs:complexType name=”category”>
      <xs:sequence>
        <xs:element name=”name” type=”xs:string”/>
        <xs:element name=”description” type=”xs:string”/>
        <xs:element name=”category” type=”category” minOccurs=”0″ maxOccurs=”unbounded”/>
        <xs:element name=”product” type=”product” minOccurs=”0″ maxOccurs=”unbounded”/>
      </xs:sequence>
      <xs:attribute name=”categoryKey” type=”xs:string” use=”required”/>
      <xs:attribute name=”parentCategoryKey” type=”xs:string” use=”required”/>
    </xs:complexType>

我们一直都在努力坚持原创.......请不要一声不吭,就悄悄拿走。

我原创,你原创,我们的内容世界才会更加精彩!

【所有原创内容版权均属TechTarget,欢迎大家转发分享。但未经授权,严禁任何媒体(平面媒体、网络媒体、自媒体等)以及微信公众号复制、转载、摘编或以其他方式进行使用。】

微信公众号

TechTarget微信公众号二维码

TechTarget

官方微博

TechTarget中国官方微博二维码

TechTarget中国

相关推荐