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

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


当前回答

我建议您先使用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调用就可以了。

请收听本期播客找出答案。如果你想在不听的情况下知道答案,那么好吧,这是REST。但我真的建议你听。

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

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

SOAP目前具有更好的工具的优势,它们可以为服务层生成大量样板代码,也可以从任何给定的WSDL生成客户端。

REST更简单,因此更容易维护,位于Web体系结构的核心,允许更好的协议可见性,并且已被证明可以扩展到WWW本身的大小。有些框架可以帮助您构建REST服务,如Ruby on Rails,有些甚至可以帮助您编写客户端,如ADO。NET数据服务。但在大多数情况下,缺乏工具支持。

快速了解一下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