SOAP消息和WSDL之间的区别?

Jam*_*mes 102 java soap wsdl web-services

我对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>
Run Code Online (Sandbox Code Playgroud)

所有SOAP消息都是WSDL的吗?SOAP是一种接受自己的"SOAP消息"还是"WSDL"的协议?如果它们不同,那么我何时应该使用SOAP消息?何时应该使用WSDL?

对此的一些澄清将是非常棒的.

Jon*_*ono 119

每个请求都会发送一个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> 
Run Code Online (Sandbox Code Playgroud)

我们希望得到一条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>
Run Code Online (Sandbox Code Playgroud)

然后,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>
Run Code Online (Sandbox Code Playgroud)

但是WSDL还包含更多信息,关于哪些功能链接在一起以进行操作,以及哪些操作可用于服务,以及网络上可以访问服务/操作的位置.

另请参见W3带注释的WSDL示例

  • 当你说这行时"WSDL然后描述了当服务器收到它时如何处理/处理这条消息." 你不觉得这里有什么问题.我认为WSDL更能让客户知道客户端所接受的服务是什么.我不认为它指导处理或处理消息到服务器.我有道理吗? (3认同)

Mat*_*hew 75

SOAP消息是用于传输数据的XML文档.WSDL是一个XML文档,它描述了如何连接和向Web服务发出请求.

基本上,SOAP消息是您传输的数据,WSDL告诉您可以执行的操作以及如何进行调用.

在Google中进行快速搜索会产生许多其他阅读资源(之前的图书链接已经过时,为了解决此问题,我们会在评论中添加任何新建议)

只是注意到你的具体问题:

所有SOAP消息都是WSDL的吗?不,他们根本不是一回事.

SOAP是一种接受自己的"SOAP消息"还是"WSDL"的协议?不需要阅读,因为这远远不够.

如果它们不同,那么我何时应该使用SOAP消息?何时应该使用WSDL?肥皂是您应用于传输的消息/数据的结构.WSDL仅用于确定如何首先调用服务.当您第一次添加代码来调用特定的Web服务时,这通常是一次性的事情.

  • WSDL告诉您如何调用Web服务.通常,您将使用WSDL自动生成代码以调用Web服务,然后再也不使用它.略读我链接的书中的章节应该有所帮助. (6认同)

sca*_*tag 27

WSDL(Web服务定义语言)是描述Web服务的元数据文件.

操作名称,参数等等

soap消息是实际的有效负载


Jad*_*ine 25

在告诉SOAP和WSDL之间有什么区别之前我们需要定义什么是Web服务,其中两个(SOAP和WSDL)是Web服务的组件

大多数应用程序被开发用于与用户交互,用户通过接口输入或搜索数据,然后应用程序响应用户的输入.

Web服务或多或少地做同样的事情,除了Web服务应用程序仅在机器之间或应用程序与应用程序之间进行通信.通常没有直接的用户交互.

Web服务基本上是一组开放协议,用于在应用程序之间交换数据.使用开放协议使Web服务能够独立于平台.以不同编程语言编写并在不同平台上运行的软件可以使用Web服务通过诸如因特网之类的计算机网络交换数据.换句话说,Windows应用程序可以与PHP,Java和Perl应用程序以及许多其他应用程序通信,这在正常情况下是不可能的.

Web服务如何工作?

由于不同的应用程序是用不同的编程语言编写的,因此它们通常无法相互通信.Web服务通过结合使用开放协议和标准(主要是XML,SOAP和WSDL)来实现此通信.Web服务使用XML来标记数据,使用SOAP来传输消息,最后使用WSDL来描述服务的可用性.我们来看看Web服务应用程序的这三个主要组件.

简单对象访问协议(SOAP)

简单对象访问协议或SOAP是用于在应用程序之间发送和接收消息而不会遇到互操作性问题的协议(互操作性意味着运行Web服务的平台变得无关紧要).另一种具有类似功能的协议是HTTP.它用于访问网页或上网.HTTP确保您不必担心哪种Web服务器(无论是Apache还是IIS或其他任何服务器)为您查看的页面提供服务,或者您查看的页面是否是用ASP.NET或HTML创建的.

因为SOAP既用于请求又用于响应,因此其内容根据其用途略有不同.

下面是SOAP请求和响应消息的示例

SOAP请求:

POST /InStock HTTP/1.1 
Host: www.bookshop.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.bookshop.org/prices"> 
    <m:GetBookPrice> 
    <m:BookName>The Fleamarket</m:BookName> 
    </m:GetBookPrice> 
</soap:Body> 
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

SOAP响应:

POST /InStock HTTP/1.1 
Host: www.bookshop.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.bookshop.org/prices"> 
    <m:GetBookPriceResponse> 
    <m: Price>10.95</m: Price> 
    </m:GetBookPriceResponse> 
</soap:Body> 
</soap:Envelope> 
Run Code Online (Sandbox Code Playgroud)

虽然两条消息看起来都一样,但它们执行的方法不同.例如,查看上面的示例,您可以看到请求消息使用该GetBookPrice方法来获取图书价格.响应由该GetBookPriceResponse方法执行,该方法将成为您作为"请求者"将看到的消息.您还可以看到消息是使用XML组成的.

Web服务描述语言或WSDL

WSDL是一个描述Web服务的文档,还告诉您如何访问和使用它的方法.

WSDL负责了解您如何知道在Internet上偶然发现的Web服务中可用的方法.

看一下示例WSDL文件:

<?xml version="1.0" encoding="UTF-8"?> 
<definitions  name ="DayOfWeek"  
  targetNamespace="http://www.roguewave.com/soapworx/examples/DayOfWeek.wsdl" 
  xmlns:tns="http://www.roguewave.com/soapworx/examples/DayOfWeek.wsdl" 
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"  
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns="http://schemas.xmlsoap.org/wsdl/">  
  <message name="DayOfWeekInput"> 
    <part name="date" type="xsd:date"/> 
  </message> 
  <message name="DayOfWeekResponse"> 
    <part name="dayOfWeek" type="xsd:string"/> 
  </message> 
  <portType name="DayOfWeekPortType"> 
    <operation name="GetDayOfWeek"> 
      <input message="tns:DayOfWeekInput"/> 
      <output message="tns:DayOfWeekResponse"/> 
    </operation> 
  </portType> 
  <binding name="DayOfWeekBinding" type="tns:DayOfWeekPortType"> 
    <soap:binding style="document"  
      transport="http://schemas.xmlsoap.org/soap/http"/> 
    <operation name="GetDayOfWeek"> 
      <soap:operation soapAction="getdayofweek"/> 
      <input> 
        <soap:body use="encoded"  
          namespace="http://www.roguewave.com/soapworx/examples"  
          encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> 
      </input> 
      <output> 
        <soap:body use="encoded"  
          namespace="http://www.roguewave.com/soapworx/examples"   
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> 
      </output> 
    </operation> 
  </binding> 
  <service name="DayOfWeekService" > 
    <documentation> 
      Returns the day-of-week name for a given date 
    </documentation> 
    <port name="DayOfWeekPort" binding="tns:DayOfWeekBinding"> 
      <soap:address location="http://localhost:8090/dayofweek/DayOfWeek"/> 
    </port> 
  </service> 
</definitions> 
Run Code Online (Sandbox Code Playgroud)

关于WSDL文件要记住的主要事项是它为您提供:

  • Web服务的描述

  • Web服务使用的方法及其所采用的参数

  • 一种定位Web服务的方法


    小智 7

    比电话更好的比喻:通过邮购服务邮寄订购产品.WSDL文档就像解释如何创建服务提供者将接受的订单表单的说明.SOAP消息就像一个标准设计(大小,形状,结构)的信封,世界各地的邮局都知道如何处理.您将订单放入这样的信封中.网络(例如互联网)是邮政服务.你把信封放进邮件里.邮政服务的员工不看信封.有效负载XML是您在信封中附带的订单.在邮局递送信封后,Web服务提供商打开信封并处理订单.如果您已正确创建并填写表单,他们会将您订购的产品邮寄给您.