Web Service 代码示例

此 Web Service 描述语言 (WSDL) 示例和代码段说明在对调解进行编程时如何访问 Web Service 消息中的字段。

Web Service 消息定义

本主题包含 Web Service 消息示例。此消息示例的特征是使用 WSDL 表现的。WSDL 是基于 XML 的语言,用于描述企业提供的服务以及这些服务的访问方式。

本主题说明如何对调解进行编程以便处理 Web Service 消息的不同部件,这些部件通过Web Service 消息的 SDO 数据图映射中的服务数据对象 (SDO) V1 表示法进行描述。对于消息的每个部分,提供了该消息的 XML 描述,这些 XML 描述代表了该消息的 SDO 数据图。每个 XML 描述都伴随一些代码段,它们举例说明了消息的该部分的处理方式。

注: 在以下示例中,SOAP 头模式包括在 WSDL 中。它也可作为独立的模式包括在 SDO 存储库中。

以下是消息的 WSDL 描述,后续的代码段将其用作示例:

companyInfo Web Service 消息描述

<wsdl:definitions targetNamespace="http://example.companyInfo"
 xmlns:tns="http://example.companyInfo"
 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
 xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:wsdlmime="http://schemas.xmlsoap.org/wsdl/mime/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <wsdl:types>
  <xsd:schema elementFormDefault="qualified"
   targetNamespace="http://example.header">

   <xsd:element name="sampleHeader">
    <xsd:complexType>
     <xsd:all>
      <xsd:element name="priority" type="xsd:int"/>
     </xsd:all>
    </xsd:complexType>
   </xsd:element>
  </xsd:schema>


  <xsd:schema elementFormDefault="qualified"
   targetNamespace="http://example.companyInfo">

   <xsd:element name="getCompanyInfo">
    <xsd:complexType>
     <xsd:all>
      <xsd:element name="tickerSymbol" type="xsd:string"/>
     </xsd:all>
    </xsd:complexType>
   </xsd:element>

   <xsd:element name="getCompanyInfoResult">
    <xsd:complexType>
     <xsd:all>
      <xsd:element name="result" type="xsd:float"/>
     </xsd:all>
    </xsd:complexType>
   </xsd:element>
  </xsd:schema>
  
  
 </wsdl:types>

 <wsdl:message name="getCompanyInfoRequest">
   <wsdl:part name="part1" element="tns:getCompanyInfo"/>
 </wsdl:message>

 <wsdl:message name="getCompanyInfoResponse">
  <wsdl:part name="part1" element="tns:getCompanyInfoResult"/>
  <wsdl:part name="part2" type="xsd:string"/>
  <wsdl:part name="part3" type="xsd:base64Binary"/>
 </wsdl:message>

 <wsdl:portType name="CompanyInfo">
  <wsdl:operation name="GetCompanyInfo">
   <wsdl:input message="tns:getCompanyInfoRequest"
               name="getCompanyInfoRequest"/>
   <wsdl:output message="tns:getCompanyInfoResponse"
                name="getCompanyInfoResponse"/>
  </wsdl:operation>
 </wsdl:portType>

 <wsdl:binding name="CompanyInfoBinding" type="tns:CompanyInfo">
  <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

  <wsdl:operation name="GetCompanyInfo">
   <wsdlsoap:operation soapAction=""/>
   <wsdl:input name="getCompanyInfoRequest">
    <wsdlsoap:body use="literal"/>
   </wsdl:input>
   <wsdl:output name="getCompanyInfoResponse">
    <wsdlsoap:body use="literal"/>
   </wsdl:output>
  </wsdl:operation>
 </wsdl:binding>

 <wsdl:service name="CompanyInfoService">
  <wsdl:port binding="tns:CompanyInfoBinding" name="SOAPPort">
   <wsdlsoap:address location="http://somewhere/services/CompanyInfoService"/>
  </wsdl:port>
 </wsdl:service>

</wsdl:definitions>

处理 info 节点

这是一个简单 SOAP 请求的示例:
<env:Envelope
  xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'
  xmlns:ns1='http://example.companyInfo'>
    <env:Body>
        <ns1:getCompanyInfo>
            <ns1:tickerSymbol>IBM</ns1:tickerSymbol>
        </ns1:getCompanyInfo>
    </env:Body>
</env:Envelope>
可使用类似于以下内容的代码段来访问 info 节点的属性(请参阅Web Service 消息的整体布局):
	// Get the info node (a child of the graph root object)
	DataObject rootNode = graph.getRootObject();
	DataObject infoNode = rootNode.getDataObject("Info");
  
  // Query the operationName, and messageType.
  String opName = infoNode.getString("operationName");
  String type   = infoNode.getString("messageType");

处理头

这是一个包括头的 SOAP 请求的示例:
<env:Envelope
  xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'
  xmlns:ns1='http://example.companyInfo'>
    <env:Header>
        <example:sampleHeader
          env:mustUnderstand='1'
          xmlns:example='http://example.header'>
            <example:priority>4</example:priority>
        </example:sampleHeader>
    </env:Header>
    <env:Body>
        <ns1:getCompanyInfo>
            <ns1:tickerSymbol>IBM</ns1:tickerSymbol>
        </ns1:getCompanyInfo>
    </env:Body>
</env:Envelope>
有关带有头列表的头条目的属性的描述,请参阅头条目。要使用头条目及其属性,请使用如下代码:
	 	// Get the info node (a child of the graph root object)
    DataObject rootNode = graph.getRootObject();
    DataObject infoNode = rootNode.getDataObject("Info");
    
    // Access the list of headers
    List headerEntries = infoNode.getList("headers");
    
    // Get the first entry from the list
    DataObject headerEntry = (DataObject) headerEntries.get(0);
    
    // Query the mustUnderstand property of the header entry
    boolean mustUnderstand = headerEntry.getBoolean("mustUnderstand");
    
    // Get the Sequence that holds the content of the header entry
    Sequence headerContent = headerEntry.getSequence("any");
    
    // Get the first piece of content from the Sequence
    DataObject header = (DataObject) headerContent.getValue(0);
    
    // Read the priority from the header
    int priority = header.getInt("priority");
    
    // Shorthand for the above, using SDO path expressions that start
    // from the info node.
    mustUnderstand = infoNode.getBoolean("headers[1]/mustUnderstand");
    priority       = infoNode.getInt("headers[1]/any[1]/priority");

处理附件

这是一个包括 XML 附件的 SOAP 请求的示例:
Content-Type: multipart/related; start="<start>"; boundary="boundary"


--boundary
Content-Type: text/xml
Content-Transfer-Encoding: 7bit
Content-ID: <start>

<env:Envelope
  xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'
  xmlns:ns1='http://example.companyInfo'>
    <env:Body>
        <ns1:getCompanyInfo>
            <ns1:tickerSymbol>IBM</ns1:tickerSymbol>
        </ns1:getCompanyInfo>
    </env:Body>
</env:Envelope>
--boundary
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-ID: <myAttachment>

<info>Some attached information</info>
--boundary--
有关带有字节数组的附件条目的属性的描述,请参阅附件条目。要使用头条目及其属性,请使用如下代码:
	 // Get the info node (a child of the graph root object)
    DataObject rootNode = graph.getRootObject();
    DataObject infoNode = rootNode.getDataObject("Info");
    
    // Access the list of attachments
    List attachmentEntries = infoNode.getList("attachments");
    
    // Get the first entry from the list
    DataObject attachmentEntry = (DataObject) attachmentEntries.get(0);
    
    // Query the contentId property of the header entry
    String contentId = attachmentEntry.getString("contentId");
    
    // Get the data contained in the attachment
    byte[] data = attachmentEntry.getBytes("data");

处理消息体

这是一个简单 SOAP 请求的示例:
<env:Envelope
  xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'
  xmlns:ns1='http://example.companyInfo'>
    <env:Body>
        <ns1:getCompanyInfo>
            <ns1:tickerSymbol>IBM</ns1:tickerSymbol>
        </ns1:getCompanyInfo>
    </env:Body>
</env:Envelope>
要查看主体的属性,请参阅消息体布局。要处理主体的内容,请使用如下代码:
    // Get the info node (a child of the graph root object)
    DataObject rootNode = graph.getRootObject();
    DataObject infoNode = rootNode.getDataObject("Info");

    // Get hold of the body node
    DataObject bodyNode = infoNode.getDataObject("body");
    
    // Get hold of the data object for the first part of the body
    DataObject part1Node = bodyNode.getDataObject("part1");
    
    // Query the tickerSymbol
    String ticker = part1Node.getString("tickerSymbol");
    
    // Shorthand for the above, using a SDO path expression that 
    // starts from the info node.
    ticker = infoNode.getString("body/part1/tickerSymbol");

指示主题类型的图标 参考主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=rjy1112
文件名:rjy1112.html