是否允许从 REST API 中的多个端点访问相同的资源?

Is it allowed to access same resource from multiple endpoints in REST API?

我有两个端点

{ path: "/pages", description: "Retrieves information about all public pages. If user is logged in, the call would return all pages that belong to the user and that are public.", arguments: "page, owner_id, creation_date, page_id, status, author", },

{ path: "/pages/pageId", description: "Retrieves information about specific page.", arguments: "page, owner_id, creation_date, status, author", }

我可以使用 /pages?pageId=xxxxx 和 /pages/pageId 检索页面。这会破坏 DX(开发人员体验)或任何其他 REST 约定(如一致性)吗?

I could retrieve a page using /pages?pageId=xxxxx and /pages/pageId. Would this broke DX (developer experience) or any other REST convention (like consistency)?

从 REST 的角度来看,这没有错 - 我们可能拥有多个具有相同表示的资源(每个资源都有自己的标识符)是正常的。

For example, the "authors' preferred version" of an academic paper is a mapping whose value changes over time, whereas a mapping to "the paper published in the proceedings of conference X" is static. These are two distinct resources, even if they both map to the same value at some point in time. The distinction is necessary so that both resources can be identified and referenced independently. A similar example from software engineering is the separate identification of a version-controlled source code file when referring to the "latest revision", "revision number 1.2.7", or "revision included with the Orange release." -- Fielding, 2000

请注意,通用组件不会知道两个不同的资源标识符指向“相同”的表示。例如,如果我们缓存了两个资源的副本,并且我们更新了其中一个(即 POST、PUT、PATCH),通用组件将不知道另一个也有 变了。


当然,您可以设计资源,使一种拼写重定向到另一种拼写:

GET /pages?pageId=12345
307 Temporary Redirect
Location: /pages/12345

您还可以使用 HTTP 元数据表示一种资源“确实”来自不同的资源

GET /pages?pageId=12345
200 OK
Content-Location: /pages/12345
...

一个资源可以有多个标识符 (IRI) 和多个表示。表示可以依赖于授权或请求附带的任何其他内容(例如不同的请求 headers),直到它遵守无状态约束。 REST 根据统一接口约束与标准或至少建议一起工作,所以我不太关心 IRI 约定,但如果你愿意,可以遵循 nice IRI convention。通常在正则表达式的情况下你会得到超文本,所以你的客户不会关心 IRI 结构,而是使用超链接,他们使用超链接的元数据来决定它在做什么,而不是 IRI 结构,如果我们谈论它是完全不相关的关于 REST,正如 Fielding 所描述的那样。