REST是更好的Web服务方法还是SOAP?或者它们是针对不同问题的不同工具?或者这是一个微妙的问题——也就是说,一个人在某些领域比另一个人稍微好一点,等等?

我尤其希望了解这些概念以及它们与php世界以及现代高端网络应用程序的关系。


当前回答

快速了解一下2012年的问题:

REST在以下方面发挥了很好的作用:

Limited bandwidth and resources. Remember the return structure is really in any format (developer defined). Plus, any browser can be used because the REST approach uses the standard GET, PUT, POST, and DELETE verbs. Again, remember that REST can also use the XMLHttpRequest object that most modern browsers support today, which adds an extra bonus of AJAX. Totally stateless operations. If an operation needs to be continued, then REST is not the best approach and SOAP may fit it better. However, if you need stateless CRUD (Create, Read, Update, and Delete) operations, then REST is it. Caching situations. If the information can be cached because of the totally stateless operation of the REST approach, this is perfect.That covers a lot of solutions in the above three.

那么我为什么要考虑SOAP呢?同样,SOAP是相当成熟和定义良好的,并且带有完整的规范。REST方法就是这样一种方法,它对开发非常开放,所以如果你具备以下条件,那么SOAP就是一个很好的解决方案:

Asynchronous processing and invocation. If your application needs a guaranteed level of reliability and security then SOAP 1.2 offers additional standards to ensure this type of operation. Things like WSRM – WS-Reliable Messaging. Formal contracts. If both sides (provider and consumer) have to agree on the exchange format then SOAP 1.2 gives the rigid specifications for this type of interaction. Stateful operations. If the application needs contextual information and conversational state management then SOAP 1.2 has the additional specification in the WS* structure to support those things (Security, Transactions, Coordination, etc). Comparatively, the REST approach would make the developers build this custom plumbing.

http://www.infoq.com/articles/rest-soap-when-to-use-each

其他回答

REST是Roy Fielding发明的一种架构,并在他的论文《架构风格和基于网络的软件架构设计》中进行了描述。Roy也是HTTP协议的主要作者,该协议定义了在万维网上的文档传输。HTTP是RESTful协议。当开发人员谈论“使用REST Web服务”时,可能更准确的说法是“使用HTTP”。

SOAP是一种基于xml的协议,它在HTTP请求/响应中进行隧道传输,因此即使您使用SOAP,您也在使用REST。关于SOAP是否为基本HTTP添加了任何重要的功能存在一些争论。

在创建Web服务之前,我建议学习HTTP。很可能您的需求可以使用规范中已经定义的功能实现,因此不需要其他协议。

我建议您先使用REST——如果您正在使用Java,请查看JAX-RS和Jersey实现。REST更简单,易于在多种语言中进行互操作。

正如其他人在这篇文章中所说,SOAP的问题在于当其他WS-*规范加入时它的复杂性,如果您误入WSDL、xsd、SOAP、WS- addressing等的错误部分,就会出现无数的互操作问题。

判断REST和SOAP之争的最好方法是看看互联网——几乎所有网络领域的大玩家,谷歌、amazon、ebay、twitter等——都倾向于使用并偏爱RESTful api而不是SOAP api。

使用REST的另一个很好的方法是,你可以在web应用程序和REST前端之间重用大量的代码和基础设施。例如,使用JAX-RS和隐式视图等框架,将资源的HTML、XML和JSON呈现出来通常是非常容易的,而且使用web浏览器也很容易处理RESTful资源

这是微妙的。

如果您需要让其他系统与您的服务进行接口,那么由于您使用契约、WSDL和SOAP标准所拥有的“验证”层,许多客户端将更乐于使用SOAP。

对于日常系统对系统的调用,我认为SOAP是很多不必要的开销,而简单的HTML调用就可以了。

我创建了一个基准,以找出它们中哪一个更快! 我看到了这样的结果:

对于1000个请求:

休息用时3秒 SOAP耗时7秒

对于10,000个请求:

休息用时33秒 SOAP用时69秒

对于1,000,000个请求:

休息用时62秒 SOAP耗时114秒

这是个好问题……我不想让你误入歧途,所以我和你一样愿意接受别人的答案。对我来说,这实际上归结于开销成本和API的使用。在创建客户端软件时,我更喜欢使用web服务,但是我不喜欢SOAP的重量。我相信REST的重量更轻,但我不太喜欢从客户的角度使用它。

我很好奇别人是怎么想的。