json - HATEOAS中 "_embedded"的含义及用法

我正在使用支持 HATEOAS 的 Spring Data REST。我是这个范式的新手。

在来 self 的 RESTful Web 服务的 GET 响应中,我经常在名为 _embedded 的节点内收到结果。

我想知道:_embedded 节点是干什么用的?它是 REST 规范的一部分吗?还是 HATEOAS 规范的一部分?还是特定于它们的 Spring 实现?

这是 GET http://localhost:8080/mywebservice/features 的 JSON 结果示例:

{
   "_links":
   {
       "search": { "href": "http://localhost:8080/mywebservice/features/search" }
   },
   "_embedded":
   {
       "features":
       [
           {
               "feature": "GROUND",
               "name": "Terreno",
               "data_type": "String",
               "_links":
               {
                   "self"  : { "href": "http://localhost:8080/mywebservice/features/GROUND" },
                   "values": { "href": "http://localhost:8080/mywebservice/features/GROUND }
               }
           },

           ...

       ]
   }
}

我注意到我几乎总是在响应中有 _embedded 节点:如果我请求一个集合,但即使通过搜索请求单个资源(例如使用 GET http://localhost:8080/mywebservice/persons/search/findByEmail?email=example@example@.com).

只有当请求是针对特定资源时,我才获得 _embedded 节点,例如在执行 GET http://localhost:8080/mywebservice/features/GROUND.

最佳答案

既没有 REST 也没有 HATEOAS 规范。如果您愿意,两者都只是概念或建筑风格。 _embeddedHAL format 的一部分.

它旨在嵌入(原文如此!)资源,否则只会返回它们的 URI。例如 GET http://localhost:8080/mywebservice/features 应该只返回一个 URI 列表,例如 http://localhost:8080/mywebservice/features/GROUND,如果需要,您必须自己加载每个 Feature。通过利用 _embedded,所有 Feature 资源都嵌入到响应中,因此您不必单独加载它们。

https://stackoverflow.com/questions/27405637/

相关文章:

javascript - 日历结束日期错了一天

json - d3 - 读取 JSON 数据而不是 CSV 文件

.net - JSON.NET 教程

json - 遍历 JSON 文件 PowerShell

mysql - 如何在 MySQL 中编写可以解析列中 JSON 数据的查询?

c# - 类型是接口(interface)或抽象类,不能实例化

json - getJSON 与 optJSON

bash - jq 直接替换文件上的文本(如 sed -i)

json - meteor `Deps.autorun` 与 `Collection.observ

javascript - 如何使用 Angular-Translate 显示翻译?