搜索

7.2. 将标头添加到 SOAP 1.2 消息

download PDF

概述

SOAP 消息标头通过在 SOAP 1.2 消息中添加 soap12:header 元素来定义。soap12:header 元素是绑定的 输入输出和 fault 元素的可选子级。SOAP 标头成为父消息的一部分。SOAP 标头通过指定消息和消息部分来定义。每个 SOAP 标头只能包含一个消息部分,但您可以根据需要插入多个标头。

语法

例 7.3 “SOAP 标头语法” 中显示定义 SOAP 标头的语法。

例 7.3. SOAP 标头语法

<binding name="headwig">
  <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="weave">
      <soap12:operation soapAction="" style="documment"/>
      <input name="grain">
        <soap12:body ... />
        <soap12:header message="QName" part="partName"
                       use="literal|encoded"
                        encodingStyle="encodingURI"
                        namespace="namespaceURI" />
      </input>
...
</binding>

soap12:header 元素的属性在 表 7.1 “soap12:header 属性” 中进行了描述。

表 7.1. soap12:header 属性
属性描述

message

一个必需属性,用于指定在标头中插入部分的合格消息名称。

part

指定插入到 SOAP 标头中的消息部分名称的必要属性。

使用

指定消息部分是否使用编码规则编码。如果设置为 编码 消息部分,则使用由 encodingStyle 属性的值指定的编码规则编码。如果设置为 literal,则消息部分由引用的 schema 类型定义。

encodingStyle

指定用于构建消息的编码规则。

namespace

定义要分配给 header 元素的命名空间,使用 use="encoded" 序列化。

在正文和标头之间分割消息

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

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

注意

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

Example

例 7.4 “使用 SOAP 标头的 SOAP 1.2 Binding” 显示 例 7.1 “排序系统接口” 中显示的 orderWidgets 服务的修改版本。此版本会被修改,以便每个顺序都有一个 xsd:base64binary 值,放置在请求和响应标头中。标头被定义为 widgetKey 消息中的 keyVal 部分。在这种情况下,您负责添加应用程序逻辑来创建标头,因为它不是输入或输出消息的一部分。

例 7.4. 使用 SOAP 标头的 SOAP 1.2 Binding

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="widgetOrderForm.wsdl"
    targetNamespace="http://widgetVendor.com/widgetOrderForm"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
    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">
  <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="placeWidgetOrder">
      <soap12:operation soapAction="" style="document"/>
      <input name="order">
        <soap12:body use="literal"/>
        <soap12:header message="tns:widgetKey" part="keyVal"/>
      </input>
      <output name="bill">
        <soap12:body use="literal"/>
        <soap12:header message="tns:widgetKey" part="keyVal"/>
      </output>
      <fault name="sizeFault">
        <soap12:body use="literal"/>
      </fault>
  </operation>
</binding>
...
</definitions>

您可以修改 例 7.4 “使用 SOAP 标头的 SOAP 1.2 Binding”,以便标头值是输入和输出信息的一部分,如 例 7.5 “SOAP 1.2 Binding for orderWidgets with a SOAP Headers” 所示。在这种情况下,keyVal 是输入和输出消息的一部分。在 soap12:body 元素中,part 属性指定 keyVal 不应插入到正文中。但是,它将插入到标头中。

例 7.5. SOAP 1.2 Binding for orderWidgets with a SOAP Headers

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="widgetOrderForm.wsdl"
    targetNamespace="http://widgetVendor.com/widgetOrderForm"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
    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"/>
  <part name="keyVal" element="xsd1:keyElem"/>
</message>
<message name="widgetOrderBill">
  <part name="price" type="xsd:float"/>
  <part name="keyVal" element="xsd1:keyElem"/>
</message>
<message name="badSize">
  <part name="numInventory" type="xsd:int"/>
</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">
  <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="placeWidgetOrder">
      <soap12:operation soapAction="" style="document"/>
      <input name="order">
        <soap12:body use="literal" parts="numOrdered"/>
        <soap12:header message="tns:widgetOrder" part="keyVal"/>
      </input>
      <output name="bill">
        <soap12:body use="literal" parts="bill"/>
        <soap12:header message="tns:widgetOrderBill" part="keyVal"/>
      </output>
      <fault name="sizeFault">
        <soap12:body use="literal"/>
      </fault>
  </operation>
</binding>
...
</definitions>
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.