gpt4 book ai didi

REST - 统一接口(interface)到底是什么意思?

转载 作者:行者123 更新时间:2023-12-03 10:53:24 25 4
gpt4 key购买 nike

Wikipedia has:

Uniform interface

The uniform interface constraint is fundamental to the design of any REST service.[14] The uniform interface simplifies and decouples the architecture, which enables each part to evolve independently. The four guiding principles of this interface are:

Identification of resources

Individual resources are identified in requests, for example using URIs in web-based REST systems. The resources themselves are conceptually separate from the representations that are returned to the client. For example, the server may send data from its database as HTML, XML or JSON, none of which are the server's internal representation, and it is the same one resource regardless.

Manipulation of resources through these representations

When a client holds a representation of a resource, including any metadata attached, it has enough information to modify or delete the resource.

Self-descriptive messages

Each message includes enough information to describe how to process the message. For example, which parser to invoke may be specified by an Internet media type (previously known as a MIME type). Responses also explicitly indicate their cacheability.

Hypermedia as the engine of application state (A.K.A. HATEOAS)

Clients make state transitions only through actions that are dynamically identified within hypermedia by the server (e.g., by hyperlinks within hypertext). Except for simple fixed entry points to the application, a client does not assume that any particular action is available for any particular resources beyond those described in representations previously received from the server.


我正在听一个关于这个主题的讲座,讲师说:

"When someone comes up to our API, if you are able to get a customer object and you know there are order objects, you should be able to get the order objects with the same pattern that you got the customer objects from. These URI's are going to look like each other."


这让我觉得错了。 这与 URI 的外观或一致性无关,因为它是 URI 的使用方式(识别资源、通过表示、自描述消息和仇恨来操作资源)。
我认为这根本不是统一接口(interface)的意思。它到底是什么意思?

最佳答案

统一接口(interface)任何 ReSTful 架构都应该遵守的约束,实际上意味着,除了数据,服务器响应还应该宣布可用的操作和资源。
chapter 5 ("Reprensational State Transfer")他的论文,Roy Fielding声明使用统一接口(interface)的目的是:

ease and improve global architecture and the visibility of interactions


换句话说, 查询资源应该允许客户端在事先不知道的情况下请求其他操作和资源 .
JSON-API 规范 ( jsonapi.org ) 提供了一个很好的例子,它以 JSON 响应的形式对 http://example.com/articles 上的(假设的)GET HTTP 请求进行了响应。 :
{
"links": {
"self": "http://example.com/articles",
"next": "http://example.com/articles?page[offset]=2",
"last": "http://example.com/articles?page[offset]=10"
},
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
},
"comments": {
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"related": "http://example.com/articles/1/comments"
}
}
},
"links": {
"self": "http://example.com/articles/1"
}
}]
}
只需分析这个单一的响应,客户就知道:
  • 什么实体被查询(本例中为“文章”);
  • 这些实体的结构 (文章有字段:idtitleauthorcomments);
  • 如何找回相关实体(即 authorcomments );
  • 还有更多实体 “文章”类型(10,基于当前响应长度和分页链接)。

  • 对于那些对这个话题充满热情的人,我强烈推荐阅读 Roy Thomas Fielding's dissertation !

    关于REST - 统一接口(interface)到底是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25172600/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com