7.2. 在 SOAP 1.2 消息中添加标头
概述
SOAP 消息标头通过在 SOAP 1.2 消息中添加 soap12:header 元素来定义。soap12:header 元素是 输入、输出 和绑定 错误 元素的可选子项。SOAP 标头成为父消息的一部分。通过指定消息和消息部分来定义 SOAP 标头。每个 SOAP 标头只能包含一个消息部分,但您可以根据需要插入多个标头。
语法
定义 SOAP 标头的语法显示在 例 7.3 “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 Attributes” 中描述。
在正文和标头间分割消息
插入到 SOAP 标头中的消息部分可以是来自合同的任何有效的消息部分。它甚至可以从父消息中用作 SOAP 正文的部分。由于不太可能在同一消息中发送信息两次,SOAP 1.2 绑定提供了指定插入到 SOAP 正文中的消息部分的方法。
soap12:body 元素具有一个可选属性 部分,它用以空格分隔的部分名称列表。定义部分后,只有列出的消息部分才会插入到 SOAP 1.2 消息的正文中。然后,您可以将剩余的部分插入到消息的标头中。
当使用父消息的部分定义 SOAP 标头时,Apache CXF 会自动填写 SOAP 标头。
示例
例 7.4 “带有 SOAP 标头的 SOAP 1.2 绑定” 显示 例 7.1 “排序系统接口” 中显示的 orderWidgets 服务的修改版本。此版本会被修改,以便每个顺序在请求标题和响应的标头中都有一个 xsd:base64binary 值。标头定义为 widgetKey 消息中的 keyVal 部分。在本例中,您要负责添加应用程序逻辑以创建标头,因为它不是输入或输出消息的一部分。
例 7.4. 带有 SOAP 标头的 SOAP 1.2 绑定
<?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 绑定”,以便标头值是输入和输出信息的一部分,如 例 7.5 “带有 SOAP 标头的 orderWidget 的 SOAP 1.2 绑定” 所示。在本例中,keyVal 是输入和输出消息的一部分。在 soap12:body 元素中,parts 属性指定 键Val 不应插入到正文中。但是,它将插入到标头中。
例 7.5. 带有 SOAP 标头的 orderWidget 的 SOAP 1.2 绑定
<?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>