6.2. 将 SOAP 标头添加到 SOAP 1.1 Binding 中

概述

SOAP 标头通过将 soap:header 元素添加到默认 SOAP 1.1 绑定来定义。soap:header 元素是 输入输出 和绑定 错误 元素的可选子项。SOAP 标头成为父消息的一部分。通过指定消息和消息部分来定义 SOAP 标头。每个 SOAP 标头只能包含一个消息部分,但您可以根据需要插入所需数量的 SOAP 标头。

语法

定义 SOAP 标头的语法显示在 例 6.3 “SOAP 标头语法” 中。soap:headermessage 属性是要插入到标头中的部分的合格名称。part 属性是插入到 SOAP 标头中的消息部分的名称。由于 SOAP 标头始终是文档样式,因此必须使用元素来定义插入到 SOAP 标头中的 WSDL 消息部分。消息部分 属性一起描述要插入到 SOAP 标头的数据。

例 6.3. SOAP 标头语法

<binding name="headwig">
  <soap:binding style="document"
                transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="weave">
      <soap:operation soapAction="" style="document"/>
      <input name="grain">
        <soap:body ... />
        <soap:header message="QName" part="partName"/>
      </input>
...
</binding>

以及强制 消息和 部分 属性,soap :header 也支持 命名空间使用,以及 编码Style 属性。这些属性与 soap:header 的作用相同

在正文和标头间分割消息

插入到 SOAP 标头中的消息部分可以是来自合同的任何有效的消息部分。它甚至可以从父消息中用作 SOAP 正文的部分。由于不太可能在同一消息中发送信息两次,SOAP 绑定提供了指定插入到 SOAP 正文中的消息部分的方法。

soap:body 元素具有一个可选属性 部分,它用以空格分隔的部分名称列表。定义部分后,只有列出的消息部分才会插入到 SOAP 正文中。然后,您可以将剩余的部分插入到 SOAP 标头中。

注意

当使用父消息的部分定义 SOAP 标头时,Apache CXF 会自动填写 SOAP 标头。

示例

例 6.4 “带有 SOAP 标头的 SOAP 1.1 绑定” 显示 例 6.1 “排序系统接口” 中显示的 orderWidgets 服务的修改版本。此版本已被修改,每个顺序都有一个 xsd:base64binary 值,放置在请求和响应的 SOAP 标头中。SOAP 标头定义为 widgetKey 消息中的 keyVal 部分。在本例中,您负责将 SOAP 标头添加到应用程序逻辑中,因为它不是输入或输出消息的一部分。

例 6.4. 带有 SOAP 标头的 SOAP 1.1 绑定

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="widgetOrderForm.wsdl"
    targetNamespace="http://widgetVendor.com/widgetOrderForm"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="http://widgetVendor.com/widgetOrderForm"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsd1="http://widgetVendor.com/types/widgetTypes"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">

<types>
  <schema targetNamespace="http://widgetVendor.com/types/widgetTypes"
           xmlns="http://www.w3.org/2001/XMLSchema"
           xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <element name="keyElem" type="xsd:base64Binary"/>
  </schema>
</types>

<message name="widgetOrder">
  <part name="numOrdered" type="xsd:int"/>
</message>
<message name="widgetOrderBill">
  <part name="price" type="xsd:float"/>
</message>
<message name="badSize">
  <part name="numInventory" type="xsd:int"/>
</message>
<message name="widgetKey">
  <part name="keyVal" element="xsd1:keyElem"/>
</message>

<portType name="orderWidgets">
  <operation name="placeWidgetOrder">
    <input message="tns:widgetOrder" name="order"/>
    <output message="tns:widgetOrderBill" name="bill"/>
    <fault message="tns:badSize" name="sizeFault"/>
  </operation>
</portType>

<binding name="orderWidgetsBinding" type="tns:orderWidgets">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="placeWidgetOrder">
      <soap:operation soapAction="" style="document"/>
      <input name="order">
        <soap:body use="literal"/>
        <soap:header message="tns:widgetKey" part="keyVal"/>
      </input>
      <output name="bill">
        <soap:body use="literal"/>
        <soap:header message="tns:widgetKey" part="keyVal"/>
      </output>
      <fault name="sizeFault">
        <soap:body use="literal"/>
      </fault>
  </operation>
</binding>
...
</definitions>

您还可以修改 例 6.4 “带有 SOAP 标头的 SOAP 1.1 绑定”,以便标头值是输入和输出信息的一部分。