在典型的 Web 服务场景中,通常使用工具技术来处理命名空间的所有细微差别。但是有些时候,特别是在使用 SAAJ(SOAP with Attachments API for Java)为特定的 Web 服务构造 SOAP 消息时,您必须自己处理命名空间问题。在没有任何工具辅助的情况下构造消息――或是部分消息时――可以使用该技巧。
虽然命名空间看似复杂,但您真正只需要掌握的是以下一份简短的规则清单:
如果 WSDL 样式为 RPC,那么可在 WSDL 绑定的 wsdlsoap:body 元素中查看命名空间。
如果 wsdlsoap:body 有命名空间属性(且 Web 服务互操作性组织(WS-I)的 Basic Profile(参见参考资料部分)需要该属性用于 RPC 样式),那么这就是 SOAP 消息中操作元素的命名空间。
如果 wsdlsoap:body 没有命名空间,那么该操作元素不符合要求。
对于数据元素而言:
如果元素通过根元素(不是根类型)定义,那么它的命名空间就是根元素的命名空间;
如果元素不是通过根定义的,那么该元素不符合要求。
这些都是简单的规则,但如同大多数规则一样,需要对其进行少许说明。本文的其余部分将展示使用这些规则的各类实例。有两种常用类型的 Web 服务描述语言(WSDL)文件: RPC/literal 和 document/literal 封装。当然还有其它的类型,但在本文中只包含这两种。
RPC/literal WSDL
清单 1 中的 RPC/literal WSDL 有三个操作:op1、op2 和 op3。注意 WSDL 文件中用粗体强调的不同命名空间。
清单 1. RPC/literal WSDL
<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="http://apiNamespace.com"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://apiNamespace.com"
xmlns:data="http://dataNamespace.com"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/">
<types>
<schema targetNamespace="http://refNamespace.com"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://refNamespace.com">
<element type="int"/>
</schema>
<schema targetNamespace="http://dataNamespace.com"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ref="http://refNamespace.com"
xmlns:tns="http://dataNamespace.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<import namespace="http://refNamespace.com"/>
<complexType >
<sequence>
<element type="int"/>
<element type="int"/>
</sequence>
</complexType>
<element nillable="true" type="tns:Data"/>
<complexType >
<sequence>
<element ref="ref:RefDataElem"/>
</sequence>
</complexType>
</schema>
</types>
<message >
<part type="data:Data"/>
</message>
<message >
<part type="data:Data"/>
</message>
<message >
<part type="data:Data"/>
</message>
<message >
<part type="data:Data"/>
</message>
<message >
<part element="data:DataElem"/>
<part type="data:Data2"/>
</message>
<message >
<part type="data:Data2"/>
</message>
<portType >
<operation >
<input message="tns:op1Request"/>
<output message="tns:op1Response"/>
</operation>
<operation >
<input message="tns:op2Request"/>
<output message="tns:op2Response"/>
</operation>
<operation >
<input message="tns:op3Request"/>
<output message="tns:op3Response"/>
</operation>
</portType>
<binding type="tns:Sample">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation >
<wsdlsoap:operation soapAction=""/>
<input>
<wsdlsoap:body namespace= "http://apiNamespace.com" use="literal"/>
</input>
<output>
<wsdlsoap:body namespace= "http://apiNamespace.com" use="literal"/>
</output>
</operation>
<operation >
<wsdlsoap:operation soapAction=""/>
<input>
<wsdlsoap:body namespace= "http://op2Namespace.com" use="literal"/>
</input>
<output>
<wsdlsoap:body namespace= "http://op2Namespace.com" use="literal"/>
</output>
</operation>
<operation >
<wsdlsoap:operation soapAction=""/>
<input>
<wsdlsoap:body use="literal"/>
</input>
<output>
<wsdlsoap:body use="literal"/>
</output>
</operation>
</binding>
<service >
<port binding="tns:SampleSoapBinding" "Sample">
<wsdlsoap:address location= "http://localhost:9080/RPCNamespaces/services/Sample"/>
</port>
</service>
</definitions>
WS-I 遵从性
WS-I为 WSDL 定义遵从性标准。从两个方面来讲,op3 不遵从 RPC/literal WSDL:它并不在绑定的 wsdlsoap:body 中定义命名空间;它的消息部分引用了元素,而不是类型。在此提出是为了展示可以用 WS-I 的 Basic Profile 解决的一些命名空间问题。
查看用于每个操作的绑定的 wsdlsoap:body 元素中的命名空间。op1 和 op2 是规则 1.1 的实例。op3 是规则 1.2 的实例。op1 展示了使用 targetNamespace 的常规实例――在这种情况下是“http://apiNamespace.com”――作为该操作的命名空间,但是这仅仅是通常情况。op2 使用的命名空间将不会在 WSDL 中的其他任何地方被使用。op3 无需定义任何命名空间。
清单 2、3 和 4 分别展示了 op1、op2 和 op3 的 SOAP 消息。注意消息中用粗体强调的命名空间。
清单 2. op1 的 RPC/literal 请求/响应 SOAP 消息
<soapenv:Envelope xmlns:soapenv=
"http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p582:op1 xmlns:p582="http://apiNamespace.com">
<in>
<data1>1</data1>
<data2>2</data2>
</in>
</p582:op1>
</soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv=
"http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p582:op1Response xmlns:p582="http://apiNamespace.com">
<op1Return>
<data1>10</data1>
<data2>20</data2>
</op1Return>
</p582:op1Response>
</soapenv:Body>
</soapenv:Envelope>
在上文中已经提及,清单 2 中的 SOAP 消息遵从规则 1.1。op1 的命名空间为“http://apiNamespace.com”。这些消息同样遵从规则 2.2。所有参数数据都不通过根元素定义,仅仅是根类型――数据――以及它的子元素。既然没有使用根元素,那么这些元素都是不合要求的。
清单 3. op2 的 RPC/literal 请求/响应 SOAP 消息
<soapenv:Envelope xmlns:soapenv=
"http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p999:op2 xmlns:p999="http://op2Namespace.com">
<in>
<data1>3</data1>
<data2>4</data2>
</in>
</p999:op2>
</soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv=
"http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p999:op2Response xmlns:p999="http://op2Namespace.com">
<op2Return>
<data1>300</data1>
<data2>400</data2>
</op2Return>
</p999:op2Response>
</soapenv:Body>
</soapenv:Envelope>
op1 和 op2 消息中的唯一真正差别(比较清单 2 和 清单 3)是:op2 的消息说明了可以使用任意选择的命名空间;无需使用 WSDL 定义的 targetNamespace。
清单 4. op3 的 RPC/literal 请求/响应 SOAP 消息
<soapenv:Envelope xmlns:soapenv=
"http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<op3>
<p10:DataElem xmlns:p10= "http://dataNamespace.com">
<data1>5</data1>
<data2>6</data2>
</p10:DataElem>
<in2>
<p971:RefDataElem xmlns:p971= "http://refNamespace.com">7</p971:RefDataElem>
</in2>
</op3>
</soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv=
"http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<op3Response>
<op3Return>
<p971:RefDataElem xmlns:p971="http://refNamespace.com">7</p971:RefDataElem>
</op3Return>
</op3Response>
</soapenv:Body>
</soapenv:Envelope>
op3 和其它两个操作区别很大。首先,没有定义命名空间,因此根据规则 1.2, <op3> 和 <op3Response> 标签都是不合要求的。
第二,名为 in1 的部分是根元素而不是根类型。由于它是元素部分,因此它的名称被元素名称 DataElem 取代。由于使用了元素名称,则根据规则 2.1,必须使用元素的命名空间“http://dataNamespace.com”。
最后,规则 2.1 再次为 in2 的类型元素而调用。in2 引用了根元素:RefDataElem。该元素用其它命名空间定义:“http://refNamespace.com”。
document/literal 封装的 WSDL
清单 5 中的 WSDL 和清单 1 中的 WSDL 是等价的。它是 document/literal 封装而不是 RPC/literal。为这个 WSDL 而生成的 Java API 和为 RPC/literal WSDL 生成的 Java API 是相同,但它们的 SOAP 消息可能还是有些差别。命名空间再次用粗体标出。
清单 5. document/literal 封装的 WSDL
<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="http://apiNamespace.com"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://apiNamespace.com"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/">
<types>
<schema targetNamespace="http://refNamespace.com"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://refNamespace.com">
<element type="int"/>
</schema>
<schema targetNamespace="http://dataNamespace.com"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ref="http://refNamespace.com"
xmlns:tns="http://dataNamespace.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<import namespace="http://refNamespace.com"/>
<complexType >
<sequence>
<element type="int"/>
<element type="int"/>
</sequence>
</complexType>
<element nillable="true" type="tns:Data"/>
<complexType >
<sequence>
<element ref="ref:RefDataElem"/>
</sequence>
</complexType>
</schema>
<schema targetNamespace="http://apiNamespace.com"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:data="http://dataNamespace.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<import namespace="http://dataNamespace.com"/>
<element >
<complexType>
<sequence>
<element type="data:Data"/>
</sequence>
</complexType>
</element>
<element >
<complexType>
<sequence>
<element type="data:Data"/>
</sequence>
</complexType>
</element>
<element >
<complexType>
<sequence>
<element type="data:Data"/>
</sequence>
</complexType>
</element>
<element >
<complexType>
<sequence>
<element type="data:Data"/>
</sequence>
</complexType>
</element>
<element >
<complexType>
<sequence>
<element ref="data:DataElem"/>
<element type="data:Data2"/>
</sequence>
</complexType>
</element>
<element >
<complexType>
<sequence>
<element type="data:Data2"/>
</sequence>
</complexType>
</element>
</schema>
</types>
<message >
<part element="tns:op1" />
</message>
<message >
<part element="tns:op1Response" />
</message>
<message >
<part element="tns:op2" />
</message>
<message >
<part element="tns:op2Response" />
</message>
<message >
<part element="tns:op3" />
</message>
<message >
<part element="tns:op3Response" />
</message>
<portType >
<operation >
<input message="tns:op1Request"/>
<output message="tns:op1Response"/>
</operation>
<operation >
<input message="tns:op2Request"/>
<output message="tns:op2Response"/>
</operation>
<operation >
<input message="tns:op3Request"/>
<output message="tns:op3Response"/>
</operation>
</portType>
<binding type="tns:Sample">
<wsdlsoap:binding style="document" transport= "http://schemas.xmlsoap.org/soap/http"/>
<operation >
<wsdlsoap:operation soapAction=""/>
<input>
<wsdlsoap:body namespace=
"http://apiNamespace.com" use="literal"/>
</input>
<output>
<wsdlsoap:body namespace=
"http://apiNamespace.com" use="literal"/>
</output>
</operation>
<operation >
<wsdlsoap:operation soapAction=""/>
<input>
<wsdlsoap:body namespace=
"http://op2Namespace.com" use="literal"/>
</input>
<output>
<wsdlsoap:body namespace=
"http://op2Namespace.com" use="literal"/>
</output>
</operation>
<operation >
<wsdlsoap:operation soapAction=""/>
<input>
<wsdlsoap:body use="literal"/>
</input>
<output>
<wsdlsoap:body use="literal"/>
</output>
</operation>
</binding>
<service >
<port binding="tns:SampleSoapBinding" >
<wsdlsoap:address location= "http://localhost:9080/Wrapped/services/Sample"/>
</port>
</service>
</definitions>
对于 op1 而言,用于 document/literal 封装服务的 SOAP 消息和用于 RPC/literal 服务的 SOAP 消息是相同的。这是最常见的一类 document/literal 封装操作。请注意此时遵从的是规则 2.1 而不是规则 1.1;命名空间是从元素而不是从 wsdl:soapbody 获得。请注意 WSDL 中的约定,封装元素和 WSDL 自身一样,都是用相同的命名空间定义:“http://apiNamespace.com”。它们可以用任意的命名空间定义,根据该约定,document/literal 封装的消息和 RPC/literal 消息是相同的。
<soapenv:Envelope xmlns:soapenv=
"http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p582:op2 xmlns:p582="http://apiNamespace.com">
<in>
<data1>3</data1>
<data2>4</data2>
</in>
</q0:op2>
</soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv=
"http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p582:op2Response xmlns:p582="http://apiNamespace.com">
<op2Return>
<data1>300</data1>
<data2>400</data2>
</op2Return>
</p582:op2Response>
</soapenv:Body>
</soapenv:Envelope>
WS-I 遵从性
WS-I 要求 document/literal 绑定不应在其 wsdl:soapbody 中拥有命名空间属性。所以 op1 和 op2 都不符合要求。我在 IBM® WebSphere® Studio Application Developer 中运行该 WSDL,IBM® WebSphere® Studio Application Developer 接受不合要求的 WSDL,但可通过忽略任何 wsdl:soapbody 命名空间来强制符合 WS-I。
从 op2 的 document/literal 封装的消息可以更明显的看出,您遵循的是规则 2.1 而不是规则 1.1。此时完全忽略了绑定的 wsdl:soapbody 中的命名空间。
最后,对比 RPC/literal op3 的 SOAP 消息(清单 4)和 document/literal 封装的 op3 SOAP 消息(清单 7)。和 op2 一样,唯一的差别是 op3 的命名空间。 RPC 版本没有命名空间,但是 document 版本有命名空间;按照规则 2.1,如同其它所有的 document 消息一样,它从元素的命名空间来获取命名空间。
清单 7. op3 的 Document/literal 封装的请求/响应 SOAP 消息
<soapenv:Envelope xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p582:op3 xmlns:p582="http://apiNamespace.com">
<p10:DataElem xmlns:p10="http://dataNamespace.com">
<data1>5</data1>
<data2>6</data2>
</p10:DataElem>
<in2>
<p971:RefDataElem xmlns:p971= "http://refNamespace.com">7</p971:RefDataElem>
</in2>
</p582:op3>
</soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p582:op3Response xmlns:p582="http://apiNamespace.com">
<op3Return>
<p971:RefDataElem xmlns:p971= "http://refNamespace.com">7</p971:RefDataElem>
</op3Return>
</p582:op3Response>
</soapenv:Body>
</soapenv:Envelope>
关于缺省命名空间
您可以使用缺省命名空间来取消对前缀的需求。例如,如清单 8 所示,e 和 f 都在缺省命名空间中,此时定义命名空间为 urn:default;e 在缺省命名空间中的原因是因为缺省命名空间定义在 e 上,且 f 在其中的原因是因为 f 是 e 的子元素。
清单 8. 缺省命名空间使用实例
<e xmlns="urn:default">
<f/>
</e>
以下是一些建议:在实例数据中不要使用缺省命名空间。
如果使用缺省命名空间,在从上下文中取出某些内容时,例如――那么此时根本不知道该元素是否符合要求,或者该元素是否已通过某父元素定义为符合要求。
如果想使子元素符合要求,则必须引入空命名空间。例如,在清单 9 中,e 在 urn:default 命名空间中,且 f 通过看起来有些奇怪的方式,被定义为不合要求。
清单 9. 缺省命名空间中不合要求名称的实例
<e xmlns="urn:default">
<f xmlns=""/>
</e>
可对命名空间产生影响的模式属性是 elementFormDefault。该属性的缺省设置是“unqualified”,到目前为止您已经看到的,子元素是不合要求的。但是如果您将 elementFormDefault="qualified" 添加到 document/literal 封装的 WSDL 中的所有模式中,那么消息中所有的元素将通过其父元素的命名空间而被定义为符合要求。例如,清单 10 包含了 op3 操作的 document/literal 封装的消息,该消息来自清单 5 中的 WSDL,此时 elementFormDefault 是符合要求的。通常,您不希望使用 elementFormDefault="qualified" 属性,因为它会使消息变得臃肿。但是在一年甚至更久以前,在各厂商之间存在着互操作性问题,通过设置该属性有时可以解决这些问题。
清单 10.使用 elementFormDefault="qualified" 的 op1 Document/literal 封装请求/响应 SOAP 消息
<soapenv:Envelope xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p582:op1 xmlns:p582="http://apiNamespace.com">
<p582:in>
<p10:data1 xmlns:p10= "http://dataNamespace.com">1</p10:data1>
<p10:data2 xmlns:p10= "http://dataNamespace.com">2</p10:data2>
</p582:in>
</p582:op1>
</soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<p582:op1Response xmlns:p582="http://apiNamespace.com">
<p582:op1Return>
<p10:data1 xmlns:p10= "http://dataNamespace.com">10</p10:data1>
<p10:data2 xmlns:p10= "http://dataNamespace.com">20</p10:data2>
</p582:op1Return>
</p582:op1Response>
</soapenv:Body>
</soapenv:Envelope>
结束语
在一般环境下,无须考虑 SOAP 消息中的命名空间。然而,在某些情况下,例如,必须手工创建 SOAP 消息,就必须予以考虑。此时,您必须对 WSDL 如何映射到 SOAP 有深入的了解。如果按照本文中的规则操作,您就可以用合适的命名空间手写 SOAP 消息,并且不会出现问题。