我正在使用支持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
时。
答案 0 :(得分:23)
既没有REST也没有HATEOAS规范。如果愿意,两者都只是概念或建筑风格。 _embedded
是HAL format的一部分。
它打算嵌入(sic!)资源,否则只返回它们的URI。例如,GET http://localhost:8080/mywebservice/features
应该只返回URI列表,例如http://localhost:8080/mywebservice/features/GROUND
,如果需要,您必须自己加载每个Feature
。利用_embedded
所有Feature
资源都会嵌入到响应中,因此您不必单独加载它们。