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而不是SOAP。我只能假设这些人不是开发人员,或者从未真正实现过任何合理规模的REST服务。实现REST服务比实现SOAP服务花费的时间要长得多。最后,它也变得更加混乱。以下是我在99%的情况下选择SOAP的原因:

1)实现REST服务比实现SOAP服务花费无限长的时间。存在用于所有现代语言/框架/平台读取WSDL并输出代理类和客户机的工具。实现REST服务是手工完成的,并且—通过阅读文档来实现。此外,在实现这两个服务时,由于没有真正的模式或引用文档,您必须“猜测”哪些内容将通过管道返回。

2)为什么要编写一个返回XML的REST服务?唯一不同的是,使用REST时,你不知道每个元素/属性代表的类型——你只能自己实现它,并希望有一天在你认为总是int的字段中不会遇到字符串。SOAP使用WSDL定义数据结构,因此这很简单。

3)我听到过这样的抱怨:使用SOAP,你有SOAP信封的“开销”。在这个时代,我们真的需要担心几个字节吗?

4)我听说过这样一种说法:使用REST,你只需将URL弹出到浏览器中,就可以看到数据。当然,如果您的REST服务使用简单身份验证或不使用身份验证。例如,Netflix的服务使用OAuth,它要求你在提交请求之前签名和编码。

5)为什么我们需要一个“可读”的URL为每个资源?如果我们使用工具来实现服务,我们真的关心实际的URL吗?

还需要我继续说下去吗?

1.从我的经验来看。我想说REST让你可以选择访问已经构建好的URL。>一个词搜索谷歌。该URL可以用作REST的web服务。 在SOAP中,您可以创建自己的web服务并通过SOAP客户机访问它。

REST支持文本、JSON、XML格式。因此,两个应用程序之间的通信更加通用。而SOAP仅支持XML格式的消息通信。

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

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

(哦,你可以使用cookie和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