java - SOAP 消息和 WSDL 之间的区别?

标签 java web-services soap wsdl

我对 SOAP 消息和 WSDL 如何结合在一起感到困惑?我已经开始研究 SOAP 消息,例如:

    POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
  <m:GetStockPrice>
    <m:StockName>IBM</m:StockName>
  </m:GetStockPrice>
</soap:Body>

</soap:Envelope>

所有 SOAP 消息都是 WSDL 的吗? SOAP 是接受自己的“SOAP 消息”或“WSDL”的协议(protocol)吗?如果它们不同,那么我应该什么时候使用 SOAP 消息,什么时候应该使用 WSDL?

对此进行一些澄清会很棒。

最佳答案

每个请求都会发送一个 SOAP 文档。假设我们是一家书店,并且有一个我们查询的远程服务器以了解特定书籍的当前价格。假设我们需要将图书的标题、页数和 ISBN 号传递给服务器。

每当我们想知道价格时,我们都会发送一条独特的 SOAP 消息。它看起来像这样;

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <m:GetBookPrice xmlns:m="http://namespaces.my-example-book-info.com">
      <ISBN>978-0451524935</ISBN>
      <Title>1984</Title>
      <NumPages>328</NumPages>
    </m:GetBookPrice>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope> 

我们期望得到一个 SOAP 响应消息,例如;

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <m:GetBookPriceResponse xmlns:m="http://namespaces.my-example-book-info.com">
      <CurrentPrice>8.99</CurrentPrice>
      <Currency>USD</Currency>
    </m:GetBookPriceResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

然后,WSDL 描述了当服务器接收到该消息时如何处理/处理该消息。在我们的例子中,它描述了 Title、NumPages 和 ISBN 的类型,我们是否应该期待 GetBookPrice 消息的响应以及该响应应该是什么样子。

类型看起来像这样;

<wsdl:types>

  <!-- all type declarations are in a chunk of xsd -->
  <xsd:schema targetNamespace="http://namespaces.my-example-book-info.com"
    xmlns:xsd="http://www.w3.org/1999/XMLSchema">

    <xsd:element name="GetBookPrice">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="ISBN" type="string"/>
          <xsd:element name="Title" type="string"/>
          <xsd:element name="NumPages" type="integer"/>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>

    <xsd:element name="GetBookPriceResponse">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="CurrentPrice" type="decimal" />
          <xsd:element name="Currency" type="string" />
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>

  </xsd:schema>
</wsdl:types>

但 WSDL 还包含更多信息,包括哪些功能链接在一起进行操作、服务中可用的操作以及您可以访问服务/操作的网络位置。

另见 W3 Annotated WSDL Examples

关于java - SOAP 消息和 WSDL 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14541066/

相关文章:

java - Hibernate + PostgreSQL 抛出异常 : Unknown entity

php - 如何使用session在php webservices和android之间工作

java - 使用命名空间和前缀进行 JAXB 解码

java - soap webservices - 返回错误消息?

java - SWT- 如何在 Windows 中更改 ProgressBar 颜色?

java - Java 中带有 Map<KeyType, ValueType> 的通用映射

Java组合有序集合

JAVA 调用属性等于字符串的对象数组方法的最佳方法是什么?

javascript - 网络服务和asp

php - Magento V2 API 覆盖产品信息模型 V2(属性未显示)