SOAP 与 REST
本文将彻底揭开 SOAP 或 REST 的神秘面纱。作为一名软件开发人员,我至今仍记得自己为了理解 SOAP 和 REST API 背后的真相所经历的种种艰辛,因此在本文中,我将尽可能地简化它们。好了,闲话少叙,让我们开始吧。
您可以在此处访问所有示例代码
肥皂
它代表简单对象访问协议,但请忘记缩写,只需将其作为 SOAP 来学习,就像我的名字是 Ali 一样,谁知道也许它代表“人工智能语言”:),曾经有一段时间 SOAP 缩写是有意义的,但也许现在没有了。
什么是协议
如果我问你
How are you?
我怎么才能确保你会回答“很好”呢?不可能吧?
怎么样
2 + 2 =?
这次我可以肯定你的答案是4,对吧?因为我作为作者,你作为读者,都在使用同一个协议,也就是数学上的协议。这就是协议的定义。SOAP也是一种协议。
在 SOAP 协议中,为了调用一个以名称为参数的函数,并使用这样的问候消息进行响应
Greeting: function(args) {
return {result: "Hello dear " + args.name};
}
我们应该这样问
SOAP 请求
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header/>
<soap:Body>
<ns1:Greeting xmlns:ns1="urn:examples:GreetingService">
<name>Ali Alp</name>
</ns1:Greeting>
</soap:Body>
</soap:Envelope>
我们将收到如下回复
SOAP 响应
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header/>
<soap:Body>
<ns1:GreetingResponse xmlns:ns1="urn:examples:GreetingService">
<result>Hello dear Ali Alp</result>
</ns1:GreetingResponse>
</soap:Body>
</soap:Envelope>
如上所示,为了调用一个简单的 Greeting 服务,需要传输大量的元数据。与此同时,SOAP 的复杂性远不止于此,它还需要一个额外的组件才能使其正常工作。WSDL(Web 服务描述语言)是一个极其复杂的 XML 文件,SOAP Web 服务使用它来向客户端解释其服务,本质上,它声明了您需要为请求生成的 SOAP 信封的详细信息。
您可以在下面找到上面提到的问候服务的 WSDL
<definitions name = "GreetingService"
targetNamespace = "http://www.examples.com/wsdl/GreetingService.wsdl"
xmlns = "http://schemas.xmlsoap.org/wsdl/"
xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns = "http://www.examples.com/wsdl/GreetingService.wsdl"
xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
<message name = "GreetingRequest">
<part name = "userName" type = "xsd:string"/>
</message>
<message name = "GreetingResponse">
<part name = "result" type = "xsd:string"/>
</message>
<portType name = "Greeting_PortType">
<operation name = "Greeting">
<input message = "tns:GreetingRequest"/>
<output message = "tns:GreetingResponse"/>
</operation>
</portType>
<binding name = "Greeting_Binding" type = "tns:Greeting_PortType">
<soap:binding style = "rpc"
transport = "http://schemas.xmlsoap.org/soap/http"/>
<operation name = "Greeting">
<soap:operation soapAction = "Greeting"/>
<input>
<soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:GreetingService" use = "encoded"/>
</input>
<output>
<soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:GreetingService" use = "encoded"/>
</output>
</operation>
</binding>
<service name = "Greeting_Service">
<documentation>WSDL File for GreetingService</documentation>
<port binding = "tns:Greeting_Binding" name = "Greeting_Port">
<soap:address
location = "http://www.examples.com/Greeting/" />
</port>
</service>
</definitions>
可以看出,对于一个简单的服务来说,这是一个复杂的 XML 代码,但考虑到它的互操作性,将证明它的复杂性,此外还有很多工具可以为您生成 WSDL。
优点
- WS-Security(自定义安全实施支持)
- WS-原子事务
- WS-ReliableMessaging
- 独立于语言、平台和传输
- 在分布式企业环境中运行良好
- 标准化
- 以 WS 标准的形式提供重要的预构建可扩展性
- 内置错误处理
- 与某些语言产品一起使用时的自动化
缺点
- 高带宽使用率
- 学习起来复杂
- 解析和处理起来很繁重
- 仅支持 XML
休息
REST 代表“表述性状态转移”。如果你搞清楚了 SOAP,那么理解 REST 就很容易了。
REST 不是像 SOAP 这样的协议,而且它被设计为通过 HTTP 协议工作,而 SOAP 独立于任何语言、平台和传输。
在 REST 中,为了调用一个以名称为参数的函数,并使用如下问候消息进行响应
Greeting: function(args) {
return {result: "Hello dear " + args.name};
}
我们应该这样问
http://server:port/greeting?name=Ali-Alp
我们将收到如下回复
Hello dear Ali-Alp
优点
- 无需昂贵的工具即可与 Web 服务交互
- 易于学习
- 高效利用带宽(与 SOAP 相比消息大小更小)
- 快速(无需大量处理)
- 在设计理念上更接近其他Web技术
缺点
- 不太安全
- 需要数据模型的外部文档
我希望你对 SOAP 和 REST API 有更深入的理解。要解答这个谜题,我得说,其实根本就没有什么谜题。或许 SOAP 和 REST 的应用方向相同,但它们的应用场景却截然不同。经验法则是,大多数 Web 服务都应使用 REST。如果你真的掌握了 REST API 的各项功能,并且做了充分的研究,那么不妨看看 SOAP 协议。
您可以在此处访问所有示例代码
文章来源:https://dev.to/alialp/soap-vs-rest-219k