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

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


当前回答

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

对于1000个请求:

休息用时3秒 SOAP耗时7秒

对于10,000个请求:

休息用时33秒 SOAP用时69秒

对于1,000,000个请求:

休息用时62秒 SOAP耗时114秒

其他回答

我相信Don Box创建SOAP是一个笑话——“看,你可以在web上调用RPC方法”,今天当他意识到它已经成为一个臃肿的web标准的噩梦时,他会呻吟:-)

REST很好,很简单,在任何地方都可以实现(所以它更像是一个“标准”,而不是标准),又快又简单。使用REST。

快速了解一下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。但我真的建议你听。

如果您正在寻找不同系统和语言之间的互操作性,我肯定会选择REST。例如,在尝试让SOAP在. net和Java之间工作时,我遇到了很多问题。

有一件事没有提到,SOAP信封既可以包含头部,也可以包含主体部分。这使您可以使用XML的完整表达来发送和接收带外信息。据我所知,REST限制你使用HTTP头和结果代码。

(哦,你可以使用cookie和REST服务一起发送“头”类型的带外数据吗?)