在阅读了大量关于REST和SOAP之间的区别之后,我的印象是REST只是HTTP的另一种说法。有人能解释一下REST给HTTP添加了什么功能吗?
注意:我并不是在寻找REST和SOAP的比较。
在阅读了大量关于REST和SOAP之间的区别之后,我的印象是REST只是HTTP的另一种说法。有人能解释一下REST给HTTP添加了什么功能吗?
注意:我并不是在寻找REST和SOAP的比较。
当前回答
具象状态传输
REST是一组规则,遵循这些规则,您就可以构建具有特定约束集的分布式应用程序。
REST是用于交换任何(XML、JSON等)消息的协议,这些消息可以使用HTTP来传输。
特点:
它是无状态的,这意味着在理想情况下客户端和服务器之间不应该维护任何连接。 客户端负责将其上下文传递给服务器,然后服务器可以存储此上下文以处理客户端的进一步请求。例如,服务器维护的会话由客户端传递的会话标识符来标识。
无国籍的好处:
Web服务可以分别处理每个方法调用。 Web服务不需要维护客户机以前的交互。 这反过来又简化了应用程序的设计。 与TCP不同,HTTP本身是一种无状态协议,因此RESTful Web服务可以与HTTP协议无缝地工作。
无国籍的缺点:
需要在每个请求中添加一个标题形式的额外层,以保存客户机的状态。 为了安全起见,我们需要为每个请求添加一个头部信息。
REST支持的HTTP方法:
得到:/ / someotherstring字符串 它是幂等的,理想情况下应该在每次调用时返回相同的结果
把: 和GET一样。幂等性,用于更新资源。
POST:应该包含一个url和正文 用于创建资源。理想情况下,多个调用应该返回不同的结果,并应该创建多个产品。
删除: 删除服务器上的资源。
负责人:
HEAD方法和GET方法是一样的,只是服务器不能在响应中返回消息体。响应HEAD请求的HTTP报头中包含的元信息应该与响应GET请求时发送的信息相同。
选项:
此方法允许客户端确定与资源相关的选项和/或需求,或服务器的功能,而无需暗示资源操作或启动资源检索。
HTTP响应
点击这里查看所有的回答。
以下是一些重要的建议: 200 -好的 3XX -来自客户端和url重定向所需的其他信息 400 -错误请求 401 -未经授权访问 403 -禁止 请求有效,但服务器拒绝操作。用户可能没有资源的必要权限,或者可能需要某种类型的帐户。
404 -未找到 无法找到所请求的资源,但将来可能可用。允许客户端的后续请求。
405 -方法不允许 所请求的资源不支持请求方法;例如,表单上的GET请求要求通过POST显示数据,或者在只读资源上的PUT请求。
404 -未找到请求 内部服务器故障 502 -网关错误
其他回答
REST api必须是超文本驱动的
Roy Fielding的博客中有一组方法来检查你正在构建的是HTTP API还是REST API:
API designers, please note the following rules before calling your creation a REST API: A REST API should not be dependent on any single communication protocol, though its successful mapping to a given protocol may be dependent on the availability of metadata, choice of methods, etc. In general, any protocol element that uses a URI for identification must allow any URI scheme to be used for the sake of that identification. [Failure here implies that identification is not separated from interaction.] A REST API should not contain any changes to the communication protocols aside from filling-out or fixing the details of underspecified bits of standard protocols, such as HTTP’s PATCH method or Link header field. Workarounds for broken implementations (such as those browsers stupid enough to believe that HTML defines HTTP’s method set) should be defined separately, or at least in appendices, with an expectation that the workaround will eventually be obsolete. [Failure here implies that the resource interfaces are object-specific, not generic.] A REST API should spend almost all of its descriptive effort in defining the media type(s) used for representing resources and driving application state, or in defining extended relation names and/or hypertext-enabled mark-up for existing standard media types. Any effort spent describing what methods to use on what URIs of interest should be entirely defined within the scope of the processing rules for a media type (and, in most cases, already defined by existing media types). [Failure here implies that out-of-band information is driving interaction instead of hypertext.] A REST API must not define fixed resource names or hierarchies (an obvious coupling of client and server). Servers must have the freedom to control their own namespace. Instead, allow servers to instruct clients on how to construct appropriate URIs, such as is done in HTML forms and URI templates, by defining those instructions within media types and link relations. [Failure here implies that clients are assuming a resource structure due to out-of band information, such as a domain-specific standard, which is the data-oriented equivalent to RPC’s functional coupling]. A REST API should never have “typed” resources that are significant to the client. Specification authors may use resource types for describing server implementation behind the interface, but those types must be irrelevant and invisible to the client. The only types that are significant to a client are the current representation’s media type and standardized relation names. [ditto] A REST API should be entered with no prior knowledge beyond the initial URI (bookmark) and set of standardized media types that are appropriate for the intended audience (i.e., expected to be understood by any client that might use the API). From that point on, all application state transitions must be driven by client selection of server-provided choices that are present in the received representations or implied by the user’s manipulation of those representations. The transitions may be determined (or limited by) the client’s knowledge of media types and resource communication mechanisms, both of which may be improved on-the-fly (e.g., code-on-demand). [Failure here implies that out-of-band information is driving interaction instead of hypertext.]
REST是SOAP的轻版本,不添加糖或着色剂。 SOAP和REST的目标都是允许用不同语言编写并使用不同通信协议的信息系统之间进行通信。
SOAP使用API契约来公开它的服务,并定义客户端调用服务的方式,应该发送什么参数以及预期的结果,而REST则不使用API契约,对于客户端来说,要知道存在什么服务以及如何调用它们,应该查看REST API文档(这可以在OpenAPI或Swagger的yml文件中定义)。
其次,SOAP是冗长的,它使用XML发送请求并描述服务、参数和返回的结果。另一方面,REST依赖简单的JSON对象来发送请求和接收结果。JSON易于理解,轻量级,并且在发送请求或接收结果时不会占用太多带宽。
具象状态传输
REST是一组规则,遵循这些规则,您就可以构建具有特定约束集的分布式应用程序。
REST是用于交换任何(XML、JSON等)消息的协议,这些消息可以使用HTTP来传输。
特点:
它是无状态的,这意味着在理想情况下客户端和服务器之间不应该维护任何连接。 客户端负责将其上下文传递给服务器,然后服务器可以存储此上下文以处理客户端的进一步请求。例如,服务器维护的会话由客户端传递的会话标识符来标识。
无国籍的好处:
Web服务可以分别处理每个方法调用。 Web服务不需要维护客户机以前的交互。 这反过来又简化了应用程序的设计。 与TCP不同,HTTP本身是一种无状态协议,因此RESTful Web服务可以与HTTP协议无缝地工作。
无国籍的缺点:
需要在每个请求中添加一个标题形式的额外层,以保存客户机的状态。 为了安全起见,我们需要为每个请求添加一个头部信息。
REST支持的HTTP方法:
得到:/ / someotherstring字符串 它是幂等的,理想情况下应该在每次调用时返回相同的结果
把: 和GET一样。幂等性,用于更新资源。
POST:应该包含一个url和正文 用于创建资源。理想情况下,多个调用应该返回不同的结果,并应该创建多个产品。
删除: 删除服务器上的资源。
负责人:
HEAD方法和GET方法是一样的,只是服务器不能在响应中返回消息体。响应HEAD请求的HTTP报头中包含的元信息应该与响应GET请求时发送的信息相同。
选项:
此方法允许客户端确定与资源相关的选项和/或需求,或服务器的功能,而无需暗示资源操作或启动资源检索。
HTTP响应
点击这里查看所有的回答。
以下是一些重要的建议: 200 -好的 3XX -来自客户端和url重定向所需的其他信息 400 -错误请求 401 -未经授权访问 403 -禁止 请求有效,但服务器拒绝操作。用户可能没有资源的必要权限,或者可能需要某种类型的帐户。
404 -未找到 无法找到所请求的资源,但将来可能可用。允许客户端的后续请求。
405 -方法不允许 所请求的资源不支持请求方法;例如,表单上的GET请求要求通过POST显示数据,或者在只读资源上的PUT请求。
404 -未找到请求 内部服务器故障 502 -网关错误
HTTP是一种在网络上传输消息的通信协议。 SOAP是一种用于交换基于xml的消息的协议,这些消息可以使用HTTP传输这些消息。 Rest是用于交换任何(XML或JSON)消息的协议,这些消息可以使用HTTP来传输。
HTTP是一种用于通信的协议,通常用于与互联网资源或web浏览器客户端的任何应用程序通信。
REST意味着您在设计应用程序时使用的主要概念是资源:对于您想要执行的每个操作,您需要定义一个资源,您通常只在该资源上执行CRUD操作,这是一个简单的任务。为此,使用HTTP协议中使用的四个动词来对抗四个CRUD操作非常方便(GET用于读取,POST用于创建,PUT用于更新,DELETE用于删除)。
这与RPC(远程过程调用)的旧概念不同,在RPC中,作为用户调用的结果,您有一组希望执行的操作。例如,如果你考虑如何在一个帖子上描述一个Facebook点赞,使用RPC,你可能会创建名为AddLikeToPost和RemoveLikeFromPost的服务,并管理它与所有与Facebook帖子相关的其他服务,因此你不需要为点赞创建特殊对象。
使用REST,您将拥有一个Like对象,它将与Delete和Create函数分开管理。这也意味着它将在数据库中描述一个单独的实体。这看起来可能是一个很小的差异,但这样工作通常会产生更简单的代码和更简单的应用程序。通过这种设计,应用程序的大部分逻辑从对象的结构(模型)中是显而易见的,不像RPC,你通常必须显式地添加更多的逻辑。
设计RESTful应用程序通常要困难得多,因为它要求您以简单的方式描述复杂的事情。仅使用CRUD函数描述所有功能是很棘手的,但这样做之后,您的生活将简单得多,并且您将发现您编写的方法更短。
One more restraint REST architecture presents is not to use a session context when communicating with a client (stateless), meaning all the information needed to understand who is the client and what he wants is passed with the web message. Each call to a function is self-descriptive, there is no previous conversation with the client which can be referenced in the message. Therefore, a client could not tell you "give me the next page" since you don't have a session to store what is the previous page and what kind of page you want, the client would have to say "my name is Yuval, get me page 2 of a specific post in a specific forum". This means a bit more data would have to transfer in the communication, but think of the difference between finding a bug reported from the "get me the next page" function in oppose to "get me page 2 of question ID 2190836 in stack overflow".
当然还有很多,但在我看来,这些是茶匙的主要概念。