有人能用通俗易懂的英语解释一下什么是REST,什么是SOAP吗?Web服务是如何工作的?


当前回答

REST

I understand the main idea of REST is extremely simple. We have used web browsers for years and we have seen how easy, flexible, performing, etc web sites are. HTML sites use hyperlinks and forms as the primary means of user interaction. Their main goal is to allow us, clients, to know only those links that we can use in the current state. And REST simply says 'why not use the same principles to drive computer rather than human clients through our application?' Combine this with the power of the WWW infrastructure and you'll get a killer tool for building great distributed applications.

另一种可能的解释是针对具有数学思维的人。每个应用程序基本上都是一个状态机,业务逻辑操作是状态转换。REST的思想是将每个转换映射到某个资源的请求上,并向客户端提供表示当前状态下可用转换的链接。因此,它通过表示和链接对状态机建模。这就是为什么它被称为具象状态传输。

It's quite surprising that all answers seem to focus either on message format, or on HTTP verbs usage. In fact, the message format doesn't matter at all, REST can use any one provided that the service developer documents it. HTTP verbs only make a service a CRUD service, but not yet RESTful. What really turns a service into a REST service are hyperlinks (aka hypermedia controls) embedded into server responses together with data, and their amount must be enough for any client to choose the next action from those links.

不幸的是,除了Roy Fielding的论文,在Web上很难找到关于REST的正确信息。(他是REST的发明人)。我推荐《REST in Practice》这本书,因为它提供了一个全面的循序渐进的教程,教你如何从SOAP演进到REST。

SOAP

这是RPC(远程过程调用)体系结构样式的一种可能形式。本质上,它只是一种允许客户端通过服务边界(网络、进程等)调用服务器方法的技术,就像调用本地方法一样。当然,它实际上与调用本地方法在速度、可靠性等方面有所不同,但思想就是这么简单。

相比

The details like transport protocols, message formats, xsd, wsdl, etc. don't matter when comparing any form of RPC to REST. The main difference is that an RPC service reinvents bicycle by designing it's own application protocol in the RPC API with the semantics that only it knows. Therefore, all clients have to understand this protocol prior to using the service, and no generic infrastructure like caches can be built because of proprietary semantics of all requests. Furthermore, RPC APIs do not suggest what actions are allowed in the current state, this has to be derived from additional documentation. REST on the other hand implies using uniform interfaces to allow various clients to have some understanding of API semantics, and hypermedia controls (links) to highlight available options in each state. Thus, it allows for caching responses to scale services and making correct API usage easily discoverable without additional documentation.

在某种程度上,SOAP(与任何其他RPC一样)试图通过服务边界进行隧道,将连接媒体视为只能传输消息的黑盒。REST决定承认Web是一个巨大的分布式信息系统,接受世界的现状并学会掌握它,而不是与之对抗。

当您同时控制服务器和客户端,并且交互不太复杂时,SOAP似乎非常适合内部网络api。开发人员更自然地使用它。然而,对于一个被许多独立方使用的公共API来说,它既复杂又庞大,REST应该更适合它。但最后这个比较非常模糊。

更新

My experience has unexpectedly shown REST development to be more difficult than SOAP. At least for .NET. While there are great frameworks like ASP.NET Web API, there's no tooling that would automatically generate client-side proxy. Nothing like 'Add Web Service Reference' or 'Add WCF Service Reference'. One has to write all serialization and service querying code by hand. And man, that's lots of boilerplate code. I think REST development needs something similar to WSDL and tooling implementation for each development platform. In fact, there seems to be a good ground: WADL or WSDL 2.0, but neither of the standards seems to be well-supported.

更新(2016年1月)

事实证明,现在有各种各样的REST API定义工具。目前我的个人偏好是RAML。

Web服务如何工作

嗯,这是一个过于宽泛的问题,因为它取决于特定web服务中使用的架构和技术。但一般来说,web服务只是web中的某个应用程序,它可以接受来自客户机的请求并返回响应。它暴露在网络上,因此它是一种网络服务,它通常是全天候可用的,这就是为什么它是一种服务。当然,它为客户端解决了一些问题(否则为什么有人会使用web服务)。

其他回答

我想这是我能解释得最简单的了。请,任何人都欢迎纠正我或补充这一点。

SOAP是一种消息格式,用于断开连接的系统(如通过internet)交换信息/数据。它处理来回的XML消息。

Web服务传输或接收SOAP消息。它们的工作方式因所用语言不同而不同。

SOAP和REST都是指不同系统相互通信的方式。

REST使用的技术类似于浏览器与web服务器之间的通信:使用GET请求一个网页,在表单字段中发布,等等。

SOAP提供了类似的功能,但是通过来回发送XML块来完成所有工作。SOAP的另一个关键组件是WSDL,它是一种描述支持哪些函数和数据元素的XML文档。可以使用wsdl以编程方式“发现”支持哪些函数,以及生成编程代码存根。

REST是一种用于设计网络应用程序的体系结构风格。其思想是,使用简单的HTTP在机器之间进行调用,而不是使用复杂的机制(如CORBA、RPC或SOAP)来连接机器。

SOAP的问题在于它与HTTP栈背后的理想相冲突。任何中间件都应该能够在不了解请求或响应内容的情况下处理HTTP请求,但例如,常规的HTTP缓存服务器如果不知道SOAP内容的哪些部分与缓存有关,则无法处理SOAP请求。SOAP只是使用HTTP作为它自己的通信协议的包装器,就像代理一样。

REST

I understand the main idea of REST is extremely simple. We have used web browsers for years and we have seen how easy, flexible, performing, etc web sites are. HTML sites use hyperlinks and forms as the primary means of user interaction. Their main goal is to allow us, clients, to know only those links that we can use in the current state. And REST simply says 'why not use the same principles to drive computer rather than human clients through our application?' Combine this with the power of the WWW infrastructure and you'll get a killer tool for building great distributed applications.

另一种可能的解释是针对具有数学思维的人。每个应用程序基本上都是一个状态机,业务逻辑操作是状态转换。REST的思想是将每个转换映射到某个资源的请求上,并向客户端提供表示当前状态下可用转换的链接。因此,它通过表示和链接对状态机建模。这就是为什么它被称为具象状态传输。

It's quite surprising that all answers seem to focus either on message format, or on HTTP verbs usage. In fact, the message format doesn't matter at all, REST can use any one provided that the service developer documents it. HTTP verbs only make a service a CRUD service, but not yet RESTful. What really turns a service into a REST service are hyperlinks (aka hypermedia controls) embedded into server responses together with data, and their amount must be enough for any client to choose the next action from those links.

不幸的是,除了Roy Fielding的论文,在Web上很难找到关于REST的正确信息。(他是REST的发明人)。我推荐《REST in Practice》这本书,因为它提供了一个全面的循序渐进的教程,教你如何从SOAP演进到REST。

SOAP

这是RPC(远程过程调用)体系结构样式的一种可能形式。本质上,它只是一种允许客户端通过服务边界(网络、进程等)调用服务器方法的技术,就像调用本地方法一样。当然,它实际上与调用本地方法在速度、可靠性等方面有所不同,但思想就是这么简单。

相比

The details like transport protocols, message formats, xsd, wsdl, etc. don't matter when comparing any form of RPC to REST. The main difference is that an RPC service reinvents bicycle by designing it's own application protocol in the RPC API with the semantics that only it knows. Therefore, all clients have to understand this protocol prior to using the service, and no generic infrastructure like caches can be built because of proprietary semantics of all requests. Furthermore, RPC APIs do not suggest what actions are allowed in the current state, this has to be derived from additional documentation. REST on the other hand implies using uniform interfaces to allow various clients to have some understanding of API semantics, and hypermedia controls (links) to highlight available options in each state. Thus, it allows for caching responses to scale services and making correct API usage easily discoverable without additional documentation.

在某种程度上,SOAP(与任何其他RPC一样)试图通过服务边界进行隧道,将连接媒体视为只能传输消息的黑盒。REST决定承认Web是一个巨大的分布式信息系统,接受世界的现状并学会掌握它,而不是与之对抗。

当您同时控制服务器和客户端,并且交互不太复杂时,SOAP似乎非常适合内部网络api。开发人员更自然地使用它。然而,对于一个被许多独立方使用的公共API来说,它既复杂又庞大,REST应该更适合它。但最后这个比较非常模糊。

更新

My experience has unexpectedly shown REST development to be more difficult than SOAP. At least for .NET. While there are great frameworks like ASP.NET Web API, there's no tooling that would automatically generate client-side proxy. Nothing like 'Add Web Service Reference' or 'Add WCF Service Reference'. One has to write all serialization and service querying code by hand. And man, that's lots of boilerplate code. I think REST development needs something similar to WSDL and tooling implementation for each development platform. In fact, there seems to be a good ground: WADL or WSDL 2.0, but neither of the standards seems to be well-supported.

更新(2016年1月)

事实证明,现在有各种各样的REST API定义工具。目前我的个人偏好是RAML。

Web服务如何工作

嗯,这是一个过于宽泛的问题,因为它取决于特定web服务中使用的架构和技术。但一般来说,web服务只是web中的某个应用程序,它可以接受来自客户机的请求并返回响应。它暴露在网络上,因此它是一种网络服务,它通常是全天候可用的,这就是为什么它是一种服务。当然,它为客户端解决了一些问题(否则为什么有人会使用web服务)。