了解WSDL中的style和use

本文对比了五种WSDL绑定类型:rpc/literal, document/literalnone-wrapper, document/literalwithwrapper, rpc/encoded, 和 document/encoded,并通过具体示例展示了不同风格下SOAP消息的构造方式。

1.   Styleuse的组合

<o:p> </o:p>

use属性可以为literalencodedstyle可以为rpcdocument,我们来对五种类型的style/use决定的binding作一比较,分别是rpc/literal, document/literal none-wrapper, document/literal with wrapper, rpc/encoded, 以及document/encoded<o:p></o:p>

从生成soap消息的角度看,rpcdocument的差别在于方法的操作名是否出现在生成的Soap消息中,encodedliteral编码方式的差别在于参数类型是否出现在生成的Soap消息中。

这样rpc/encoded能够完整地表示一个方法调用,但缺点是性能很差,而且不能校验Soap消息的有效性。

rpc/literal不对参数类型进行编码,但仍然无法对Soap消息进行校验。Document/encoded没有意义,因为没有方法名,对参数类型编码没有什么意义。

document/literal none-wrapper无法生成操作名,适用于完整的文档作为参数传递给方法的场景。

document/literal with wrapper应该是目前使用最多的方式,在xfire中也是默认配置。

2.   示例

<o:p> </o:p>

2.1. Java方法

<o:p> </o:p>

public void myMethod(int x);<o:p></o:p>

2.2. Rpc/encoded

<o:p> </o:p>

Rpc风格的特点是message元素的每一个part都必须用type属性定义。encoded用来指明一个类型编码模型,不依赖于XML Schema

清单 2. 用于 myMethod RPC/编码的 WSDL <o:p></o:p>

<message name="myMethodRequest"><o:p></o:p>

    <part name="x" type="xsd:int"/><o:p></o:p>

</message><o:p></o:p>

<message name="empty"/><o:p></o:p>

<portType name="PT"><o:p></o:p>

    <operation name="myMethod"><o:p></o:p>

        <input message="myMethodRequest"/><o:p></o:p>

        <output message="empty"/><o:p></o:p>

    </operation><o:p></o:p>

</portType><o:p></o:p>

<binding .../>  <o:p></o:p>

<!-- I won't bother with the details, just assume it's RPC/encoded. --><o:p></o:p>

<o:p> </o:p>

现在用“5”作为参数 x 的值来调用此方法。我们将发送一个与 清单3类似的 SOAP 消息。

清单 3. 用于 myMethod RPC/编码的 SOAP 消息 <o:p></o:p>

<soap:envelope><o:p></o:p>

    <soap:body><o:p></o:p>

        <myMethod><o:p></o:p>

            <x xsi:type="xsd:int">5</x><o:p></o:p>

        </myMethod><o:p></o:p>

    </soap:body><o:p></o:p>

</soap:envelope><o:p></o:p>

<o:p> </o:p>

2.3. RPC/文字

<o:p> </o:p>

用于我们的方法的 RPC/文字的 WSDL 看起来与 RPC/编码的 WSDL 几乎一样。只是绑定的用法由 编码改为 文字。仅此而已。

4. 用于 myMethod RPC/文字的 WSDL<o:p></o:p>

<message name="myMethodRequest"><o:p></o:p>

    <part name="x" type="xsd:int"/><o:p></o:p>

</message><o:p></o:p>

<message name="empty"/><o:p></o:p>

<portType name="PT"><o:p></o:p>

    <operation name="myMethod"><o:p></o:p>

        <input message="myMethodRequest"/><o:p></o:p>

        <output message="empty"/><o:p></o:p>

    </operation><o:p></o:p>

</portType><o:p></o:p>

<binding .../>  <o:p></o:p>

<!-- I won't bother with the details, just assume it's RPC/<o:p></o:p>

        literal. --><o:p></o:p>

      <o:p></o:p>

<o:p> </o:p>

RPC/文字的 SOAP 消息去掉了类型编码。

清单 5. 用于 myMethod RPC/文字的 SOAP 消息<o:p></o:p>

<soap:envelope><o:p></o:p>

    <soap:body><o:p></o:p>

        <myMethod><o:p></o:p>

            <x>5</x><o:p></o:p>

        </myMethod><o:p></o:p>

    </soap:body><o:p></o:p>

</soap:envelope><o:p></o:p>

<o:p> </o:p>

2.4. document/literal

<o:p> </o:p>

document/literal的主要特点是message元素的每一个part都使用element属性引用一个types下面定义的schema元素。<o:p></o:p>

清单6. 用于 myMethod 的文档/文字的 WSDL<o:p></o:p>

<types><o:p></o:p>

    <schema><o:p></o:p>

        <element name="xElement" type="xsd:int"/><o:p></o:p>

    </schema><o:p></o:p>

</types><o:p></o:p>

<message name="myMethodRequest"><o:p></o:p>

    <part name="x" element="xElement"/><o:p></o:p>

</message><o:p></o:p>

<message name="empty"/><o:p></o:p>

<portType name="PT"><o:p></o:p>

    <operation name="myMethod"><o:p></o:p>

        <input message="myMethodRequest"/><o:p></o:p>

        <output message="empty"/><o:p></o:p>

    </operation><o:p></o:p>

</portType><o:p></o:p>

<binding .../>  <o:p></o:p>

<!-- I won't bother with the details, just assume it's <o:p></o:p>

        document/literal. --><o:p></o:p>

      <o:p></o:p>

清单7. 用于 myMethod 的文档/文字的 SOAP 消息<o:p></o:p>

<soap:envelope><o:p></o:p>

    <soap:body><o:p></o:p>

        <xElement>5</xElement><o:p></o:p>

### WSDL in Web Service Architecture: Definition, Usage, and Examples WSDL (Web Services Description Language) is an XML-based language designed to describe web services, their functions, parameters, and return values[^1]. It serves as a standard format that both the client and server of a web service can understand. This description includes details about the operations provided by the web service, message formats for input and output data, binding information specifying protocol and data format specifics, and the service endpoint location. A WSDL document typically consists of several key components: - **Definitions**: The root element of the WSDL document that defines namespaces and imports other documents. - **Types**: Describes the data types used by the web service, often defined using XML Schema. - **Message**: Specifies the data elements for the messages exchanged between the client and server. - **Operation**: Defines the actions supported by the web service, including input, output, fault, and parameter order. - **Port Type**: A collection of operations grouped into a logical interface. - **Binding**: Specifies the communication protocols and data formats for each port type. - **Service**: Provides the contact information for the deployed web service, including endpoints. The role of WSDL in web service architecture is critical because it acts as a contract between the service provider and consumer. It allows clients to discover what operations are available, how to invoke them, and what to expect in response. Modern development tools can automatically generate WSDL documents from existing web services and also import WSDL files to create proxy classes for invoking those services[^1]. Here is an example of a simple WSDL file: ```xml <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://example.com/sample" targetNamespace="http://example.com/sample"> <types> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="Input" type="xsd:string"/> <xsd:element name="Output" type="xsd:string"/> </xsd:schema> </types> <message name="RequestMessage"> <part name="inputPart" element="tns:Input"/> </message> <message name="ResponseMessage"> <part name="outputPart" element="tns:Output"/> </message> <portType name="SamplePortType"> <operation name="SampleOperation"> <input message="tns:RequestMessage"/> <output message="tns:ResponseMessage"/> </operation> </portType> <binding name="SampleBinding" type="tns:SamplePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="SampleOperation"> <soap:operation soapAction="http://example.com/sample/SampleOperation"/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="SampleService"> <port name="SamplePort" binding="tns:SampleBinding"> <soap:address location="http://example.com/sample"/> </port> </service> </definitions> ``` This example demonstrates a basic structure of a WSDL document, showcasing its definitions, types, messages, port types, bindings, and services.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值