简体   繁体   English

Rest API Hateoas:API 响应应该将标识符作为硬编码还是作为占位符?

[英]Rest API Hateoas : Should API response have identifiers as hard coded or as placeholders?

Link to the HATEOAS This is the link to the Hateoas article (snapshot below) where the identifiers of the resource is part of the URL ie 12345. Here the API response has the final API relative URL ie /accounts/12345/deposit and the client just needs to hit it. Link to the HATEOAS This is the link to the Hateoas article (snapshot below) where the identifiers of the resource is part of the URL ie 12345. Here the API response has the final API relative URL ie /accounts/12345/deposit and the client只需要击中它。

Hateoas 维基百科图片

Link to the Github Users API This is the link to the Github API (snapshot below) where there are lots of placeholders for identifiers. Link to the Github Users API This is the link to the Github API (snapshot below) where there are lots of placeholders for identifiers. How will clients modify these URLSs and add a value in these placeholders?客户端将如何修改这些 URL 并在这些占位符中添加值? For example, {/gist_id}, {/other_user}.例如,{/gist_id}、{/other_user}。

Isn't passing the URL with id value instead of placeholder better?传递带有 id 值而不是占位符的 URL 不是更好吗? Why and when to rely on different clients to add values in these placeholders?为什么以及何时依赖不同的客户端在这些占位符中添加值?

在此处输入图像描述

Hypertext as the engine of application state (HATEOAS) is a bit more than just the usage of links.超文本作为应用程序 state (HATEOAS) 的引擎不仅仅是使用链接。 In essence it enforces the interaction model that is used on the Web for two decades quite successfully.从本质上讲,它非常成功地执行了在 Web 上使用了二十年的交互 model。 On the web a server usually "teaches" clients (browsers) to achieve something via the help of link relations , that can be used to automatically download related resources or give a hint on the reference resource, and Web forms , that define the syntax and semantics of each of the respective supported (input) elements, ie a text field, an option element to select one or multiple choices, a drop down or even a slider widget.在 web 上,服务器通常“教”客户端(浏览器)通过链接关系实现某些目标,可用于自动下载相关资源或提示参考资源,以及Web ZAC68B6236C和 8A9FECZE8ACCE 定义语法每个支持的(输入)元素的语义,即文本字段,select 一个或多个选择的选项元素,下拉菜单甚至 slider 小部件。 Based on the affordance of each of the elements a client knows ie that a button wants to be clicked or pressed while a text fields wants some user input and stuff or a link annotated with the prefetch link relation name may be downloaded automatically once the current page finished loading as a client might invoke it next or a preload link relation might instruct a user agent to load the referenced resource early in the current page loading process.基于每个元素的可供性,客户端知道,即按钮想要被点击或按下,而文本字段想要一些用户输入和东西或带有prefetch链接关系名称注释的链接可以在当前页面自动下载完成加载,因为客户端可能会在接下来调用它,或者preload链接关系可能会指示用户代理在当前页面加载过程的早期加载引用的资源。

The form not only teaches a client about the supported fields a resource has but also about the target URI to send the request to, the HTTP method to use wile sending the request as well as the media-type, which in the case of Web forms is usually implicitly set to application/x-www-form-urlencoded .该表单不仅向客户端介绍了资源具有的支持字段,还向客户端介绍了将请求发送到的目标 URI、HTTP 方法在发送请求时使用的方法以及媒体类型,在 Web ZAC68B62ABFDC68ACCE0C26 的情况下通常隐式设置为application/x-www-form-urlencoded

In an ideal world a client just uses the information given by the server.在理想世界中,客户端只使用服务器提供的信息。 Unfortunately, the world isn't perfect and over time people have come up with plenty of other solutions.不幸的是,世界并不完美,随着时间的推移,人们提出了许多其他解决方案。 Among one of them is URI templating that basically allows clients to use a basic URI and fill out certain placeholders with concrete values.其中之一是URI 模板,它基本上允许客户端使用基本 URI 并用具体值填写某些占位符。 As making use of templating requires some knowledge of the URIs intention or the parameters you need to pass, such capabilities make only sense as part of media-type support.由于使用模板需要了解 URI 意图或您需要传递的参数,因此此类功能仅作为媒体类型支持的一部分才有意义。

Plain JSON ( application/json ) has by default no support for URIs whatsoever and as such a user agent receiving a plain JSON payload might not be able to automatically replace a template URI with a concrete one out of the box.普通 JSON ( application/json ) 默认情况下不支持任何 URI,因此接收普通 JSON 有效负载的用户代理可能无法自动用开箱即用的具体 URI 替换模板 URI。 JSON Hyper-Schema ( application/schema+json ) attempts to add link and URI template support to plain JSON payloads. JSON Hyper-Schema ( application/schema+json ) 尝试将链接和 URI 模板支持添加到普通的 JSON 有效负载中。 A user client though needs to be hinted with the appropriate media-type in order to automatically resolve the full URI.但是,用户客户端需要使用适当的媒体类型来提示,以便自动解析完整的 URI。 As such, the user agent also has to support that respective media type otherwise it won't be able to process the document (resolve the template URI to a real URI) successfully.因此,用户代理还必须支持相应的媒体类型,否则它将无法成功处理文档(将模板 URI 解析为真实 URI)。

JSON Hypertext Application Language akaHAL JSON also supports URI templates for links. JSON 超文本应用语言又名HAL JSON也支持链接的 URI 模板。 application/collection+json does support two kinds of templates - query templates and objects-template . application/collection+json确实支持两种模板—— 查询模板对象模板 The primer one is similar to a URI template by allowing to append certain query parameters to the target URI upon sending the request while the latter one allows to define a whole object that contains all the input elements used to add or edit an item within the collection.引物类似于 URI 模板,允许 append 在发送请求时向目标 URI 提供某些查询参数,而后者允许定义整个 object,其中包含用于在集合中添加或编辑项目的所有输入元素. JSON-LD does not really support URI templating AFAIK though it uses the concept of a so called context where certain terms can be used to abbreviate URIs. JSON-LD并不真正支持 URI 模板 AFAIK,尽管它使用所谓的上下文的概念,其中某些术语可用于缩写 URI。 As such something like name can be used within the context for a URI like http://schema.org/name .因此,可以在诸如http://schema.org/name之类的 URI 的上下文中使用诸如name之类的东西。

As you can hopefully see, the support for URI templating depends on the media-type used for exchanging data.如您所见,对 URI 模板的支持取决于用于交换数据的媒体类型。 In the case of the outlined github example GET /users/:username this more or less resembles a typical Web API documentation, similar as it is done in a Swagger API documentation , that unfortunately has hardly anything to do with HATEOAS . In the case of the outlined github example GET /users/:username this more or less resembles a typical Web API documentation, similar as it is done in a Swagger API documentation , that unfortunately has hardly anything to do with HATEOAS .

For your top example (banking), you should absolutely include the complete URL, with account numbers (IDs), so that the client does not need to translate/substitute anything.对于您的顶级示例(银行),您绝对应该包含完整的 URL 和帐号(ID),以便客户不需要翻译/替换任何内容。 This is the most common scenario with HATEOAS.这是 HATEOAS 最常见的情况。 However, GitHub does have those "placeholders" for endpoints that could contain multiple values.但是,GitHub 确实为可能包含多个值的端点提供了那些“占位符”。 You can't include the "following_url" for every user in the response, it's not practical.您不能在响应中包含每个用户的“following_url”,这是不切实际的。 So you have to determine the "other_user" value another way and make the substitution.因此,您必须以另一种方式确定“other_user”值并进行替换。 Personally, I haven't even had this use case with any of my applications and all of my HATEOAS URLs resemble you first example (though I prefer full URLs not relative).就个人而言,我的任何应用程序都没有这个用例,而且我所有的 HATEOAS URL 都类似于您的第一个示例(尽管我更喜欢完整的 URL,而不是相对的)。 Unless you have specific cases like GitHub does, it's not necessary to use any of these placeholders.除非你有像 GitHub 这样的特殊情况,否则没有必要使用这些占位符。 Even GitHub only uses that where they could be multiple values.即使是 GitHub 也只使用它们可能是多个值的地方。 For fixed value URLs, they have the username (like your account number) in the URL ("octocat").对于固定值 URL,它们在 URL(“octocat”)中具有用户名(如您的帐号)。

According to me we should not give the direct url in the body We should always parameterized the api and get details form there.根据我的说法,我们不应该在正文中直接给出 url 我们应该始终参数化 api 并在那里获取详细信息。 In simple case if Id of data change than every time data need to update for detail url.在简单的情况下,如果数据 ID 发生变化,则每次数据都需要更新详细信息 url。 Else if it's dynamic you will never face this issue.否则,如果它是动态的,您将永远不会遇到这个问题。 And this also come under best practices.这也属于最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM