REST是更好的Web服务方法还是SOAP?或者它们是针对不同问题的不同工具?或者这是一个微妙的问题——也就是说,一个人在某些领域比另一个人稍微好一点,等等?
我尤其希望了解这些概念以及它们与php世界以及现代高端网络应用程序的关系。
REST是更好的Web服务方法还是SOAP?或者它们是针对不同问题的不同工具?或者这是一个微妙的问题——也就是说,一个人在某些领域比另一个人稍微好一点,等等?
我尤其希望了解这些概念以及它们与php世界以及现代高端网络应用程序的关系。
当前回答
从工具的角度来看,SOAP很有用,因为工具很容易使用WSDL。因此,您可以用您喜欢的语言为您生成Web服务客户机。
REST在AJAX的网页上表现得很好。如果保持请求的简单性,就可以直接从JavaScript调用服务,这非常方便。尽量避免在响应XML中使用任何名称空间,我看到浏览器在使用这些名称空间时会窒息。因此,xsi:type可能不适合您,没有过于复杂的XML schema。
REST tends to have better performance as well. CPU requirements of the code generating REST responses tend to be lower than what SOAP frameworks exhibit. And, if you have your XML generation ducks lined up on the server side, you can effectively stream XML out to the client. So, imagine you're reading rows of database cursor. As you read a row, you format it as an XML element, and you write that directly out to the service consumer. This way, you don't have to collect all of the database rows in memory before starting to write your XML output - you read and write at the same time. Look into novel templating engines or XSLT to get the streaming to work for REST.
另一方面,SOAP往往是由工具生成的服务生成的一个大blob,然后才编写。请注意,这并不是绝对的事实,有很多方法可以从SOAP中获得流特性,比如使用附件。
我的决策过程如下:如果我想让我的服务易于被消费者使用,并且我编写的消息将是中等到小型的(10MB或更小),并且我不介意在服务器上消耗一些额外的CPU周期,那么我就使用SOAP。如果我需要在web浏览器上使用AJAX,或者我需要进行流式处理,或者我的响应非常大,我就使用REST。
最后,围绕SOAP建立了许多很棒的标准,如WS-Security和获得有状态的Web服务,如果使用正确的工具,可以将它们插入。这类东西真的很重要,可以帮助您满足一些棘手的要求。
其他回答
快速了解一下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
不要忽视XML-RPC。如果您只是追求一个轻量级的解决方案,那么对于一个可以在几页文本中定义并在最少的代码中实现的协议来说,有很多事情要做。XML-RPC已经存在了多年,但已经过时了一段时间——但是极简主义的吸引力似乎使它在最近得到了某种复兴。
REST是一个体系结构,SOAP是一个协议。
这是第一个问题。
您可以在REST应用程序中发送SOAP信封。
SOAP本身实际上是相当基本和简单的,它之上的WSS-*标准使它非常复杂。
如果您的消费者是其他应用程序和其他服务器,那么目前对SOAP协议有很多支持,在现代ide中移动数据的基本操作基本上就是单击鼠标。
如果您的消费者更有可能是ria或Ajax客户端,那么您可能希望使用比SOAP更简单、更适合客户端的东西(特别是JSON)。
通过HTTP发送的JSON包不一定是REST体系结构,它只是发送到url的消息。所有这些都是完全可行的,但是REST习惯用法有一些关键组件。然而,这两者很容易混淆。但是,仅仅因为您谈论的是HTTP请求,并不一定意味着您拥有REST体系结构。您可以有一个完全没有HTTP的REST应用程序(注意,这种情况很少见)。
因此,如果您的服务器和消费者对SOAP“满意”,SOAP和WSS堆栈就可以很好地为您服务。如果你在做一些特别的事情,想要更好地与web浏览器交互,那么一些轻量级的HTTP协议也可以很好地工作。
回答2012年刷新的问题(通过第二个赏金),并回顾今天的结果(其他答案)。
SOAP的利弊
关于SOAP 1.2,与“REST”相比的优点和缺点…从2007年开始 你可以用WSDL描述REST Web服务, 并使用SOAP协议…也就是说,如果您再努力一点,web服务协议栈的所有W3C标准都可以是REST!
这是一个很好的起点,因为我们可以想象一个场景,在这个场景中,所有的哲学和方法论讨论都暂时被避免了。我们可以在技术上比较类似服务中的“SOAP-REST”和“非SOAP-REST”,
SOAP-REST (="REST-SOAP"): as showed by L.Mandel, WSDL2 can describe a REST webservice, and, if we suppose that exemplified XML can be enveloped in SOAP, all the implementation will be "SOAP-REST". NON-SOAP-REST: any REST web service that can not be SOAP... That is, "90%" of the well-knowed REST examples. Some not use XML (ex. typical AJAX RESTs use JSON instead), some use another XML strucutures, without the SOAP headers or rules. PS: to avoid informality, we can suppose REST level 2 in the comparisons.
当然,为了从概念上进行比较,可以将“NON-REST-SOAP”与“NON-SOAP-REST”作为不同的建模方法进行比较。那么,完成这个web服务分类:
NON-REST-SOAP:任何不能是REST的SOAP web服务…也就是说,“90%”的广为人知的SOAP示例。 NON-REST-NEITHER-SOAP:是的,“web服务建模”的世界包括其他东西(例如XML-RPC)。
REST条件下的SOAP
比较可比较的事物:皂歇和非皂歇。
PROS
解释一些术语,
Contractual stability: for all kinds of contracts (as "written agreements"), By the use of standars: all levels of the W3C stack are mutually compliant. REST, by other hand, is not a W3C or ISO standard, and have no normatized details about service's peripherals. So, as I, @DaveWoldrich(20 votes), @cynicalman(5), @Exitos(0) said before, in a context where are NEED FOR STANDARDS, you need SOAP. By the use of best practices: the "verbose aspect" of the W3C stack implementations, translates relevant human/legal/juridic agreements. Robustness: the safety of SOAP structure and headers. With metada communication (with the full expressiveness of XML) and verification you have an "insurance policy" against any changes or noise. SOAP have "transactional reliability (...) deal with communication failures. SOAP has more controls around retry logic and thus can provide more end-to-end reliability and service guarantees", E. Terman.
按受欢迎程度排序,
更好的工具(~70票):从2007年到2012年,SOAP目前拥有更好的工具的优势,因为它是一个定义良好且被广泛接受的标准。参见@MarkCidade(27票),@DaveWoldrich(20票),@JoshM(13票),@TravisHeseman(9票)。 标准遵从性(25票):正如我,@DaveWoldrich(20票),@愤世嫉俗的人(5票),@Exitos(0票)之前所说,在需要标准的上下文中,您需要SOAP。 健壮性:SOAP头的保险,@JohnSaunders(8票)。
CONS
SOAP strucuture is more complex (more than 300 votes): all answers here, and sources about "SOAP vs REST", manifest some degree of dislike with SOAP's redundancy and complexity. This is a natural consequence of the requirements for formal verification (see below), and for robustness (see above). "REST NON-SOAP" (and XML-RPC, the SOAP originator) can be more simple and informal. The "only XML" restriction is a performance obstacle when using tiny services (~50 votes): see json.org/xml and this question, or this other one. This point is showed by @toluju(41), and others. PS: as JSON is not a IETF standard, but we can consider a de facto standard for web software community.
使用SOAP对服务建模
现在,我们可以添加SOAP- non - rest与非SOAP- rest比较,并解释什么时候使用SOAP更好:
Need for standards and stable contracts (see "PROS" section). PS: see a typical "B2B need for standards" described by @saille. Need for tools (see "PROS" section). PS: standards, and the existence of formal verifications (see bellow), are important issues for the tools automation. Parallel heavy processing (see "Context/Foundations" section below): with bigger and/or slower processes, no matter with a bit more complexity of SOAP, reliability and stability are the best investments. Need more security: when more than HTTPS is required, and you really need additional features for protection, SOAP is a better choice (see @Bell, 32 votes). "Sending the message along a path more complicated than request/response or over a transport that does not involve HTTP", S. Seely. XML is a core issue, offering standards for XML Encryption, XML Signature, and XML Canonicalization, and, only with SOAP you can to embed these mechanisms into a message by a well-accepted standard as WS-Security. Need more flexibility (less restrictions): SOAP not need exact correspondence with an URI; not nedd restrict to HTTP; not need to restrict to 4 verbs. As @TravisHeseman (9 votes) says, if you wanted something "flexible for an arbitrary number of client technologies and uses", use SOAP.PS: remember that XML is more universal/expressive than JSON (et al). Need for formal verifications: important to understand that W3C stack uses formal methods, and REST is more informal. Your WSDL (a formal language) service description is a formal specification of your web services interfaces, and SOAP is a robust protocol that accept all possible WSDL prescriptions.
上下文
历史
要判断趋势,必须有历史的眼光。对于这个问题,10年或15年的展望……
在W3C标准化之前,有一些混乱。很难用不同的框架实现可互操作的服务,而在公司之间实现一些可互操作的东西则更加困难、昂贵和耗时。 W3C堆栈标准一直是复杂web服务集互操作的明灯。
对于日常的任务,比如实现AJAX, SOAP很繁重……因此,需要简单的方法,需要选择一个新的理论框架…而大的“网络软件玩家”,如谷歌、亚马逊、雅虎等,选出了最佳的替代方案,那就是REST方法。在这种情况下,REST概念作为一个“竞争性框架”出现了,并且,今天(2012年),这个替代方案已经成为程序员的事实上的标准。
基金会
在并行计算环境下,web服务提供并行子任务;和协议,如SOAP,确保良好的同步和通信。不是“任何任务”:web服务可以被分类为 粗粒度和令人尴尬的并行。
随着任务变得越来越大,“复杂性争论”变得不那么重要,而与通信的稳健性和合同的稳固性更加相关。
如果您正在寻找不同系统和语言之间的互操作性,我肯定会选择REST。例如,在尝试让SOAP在. net和Java之间工作时,我遇到了很多问题。