HATEOAS中“_embedded”的含义和用法

时间:2014-12-10 16:08:43

标签: json spring rest hateoas spring-hateoas

我正在使用支持HATEOAS的Spring Data REST。我对这个范例很陌生。

在我的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时。

1 个答案:

答案 0 :(得分:23)

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

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