HATEOAS 和客户端实现

HATEOAS and client implementation

我已经阅读了一些关于 HATEOAS 的文章以及 API 的实现方式,以便您可以通过链接遍历不同的状态。但是我对客户端应该如何实现感到困惑?

从这个answer:

The client knows nothing about how the server designs its URIs other than what it can find out at runtime.

为了实施 HATEOAS 服务器需要包含 links 到从服务器到客户端的响应,客户端使用这些 links 响应与服务器通信。

例如。客户端请求产品列表,服务器将响应带有 link 的产品列表以添加、编辑和删除产品(如果用户能够这样做),然后在客户端将其转换为 links或编辑产品、删除产品等按钮。

blog 可能会帮助您加深理解。

Does the client need to crawl from the root node down to a nested resource to just make a POST if it doesn't know the direct URI?

是的,除非根页面不包含描述 POST 的 link。

What would be the purpose of API documentation then?

它描述了客户端可以识别的元数据,例如 link 或 属性。所以它描述了服务的功能。

回答你的第一个问题"Does the client need to crawl from the root node down to a nested resource to just make a POST if it doesn't know the direct URI?"

答案是否定的。客户端总是不必从根节点开始爬取。可以将 URI 设计为可以从其他视图到达直接 URI 的导航方式。例如,如果 URI (/Products/product1/Delete) 的目的是删除 product ,那么应该可以通过从 /Products 抓取或通过提供 Products/Deleteview 的命名空间查询来达到此目的。此 "DeleteView" URI 应提供删除视图的所有 URI。即它可以 return uri 集合,如 Products/product1/Delete、Products/product2/Delete 等

第二个问题"What would be the purpose of API documentation then?" API 的概念是过去的遗产。理想情况下,URI 本身应该是文档。通过这种方式,客户端可以被编程为只发现和接受它可以响应的事物。

我们掌握了管理这些名称空间的独特技术。所以我们自己不受这种视图操纵的影响。我推荐使用一种技术来操作和创建这些 URI 名称空间。