"_embedded"的含义和用法.在HATEOAS [英] Meaning and usage of "_embedded" in HATEOAS

查看:510
本文介绍了"_embedded"的含义和用法.在HATEOAS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Spring Data REST, which supports HATEOAS. I'm new to this paradigm.

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

In GET responses from my RESTful web service I often receive results inside a node named _embedded.

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

I'm wondering: what is _embedded node for? Is it part of REST specification? Or part of HATEOAS specification? Or is it specific for the Spring implementation of them?

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

This is an example of JSON result for GET http://localhost:8080/mywebservice/features:

{
   "_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)请求一个资源,也是如此.

I noticed that I almost always have _embedded node in the response: if I request a collection, but even if a request a single resource by a search (for instance with GET http://localhost:8080/mywebservice/persons/search/findByEmail?email=example@example@.com).

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

I don't get _embedded node only if the request is for a specific resource, for instance when doing GET http://localhost:8080/mywebservice/features/GROUND.

推荐答案

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

There's neither a REST nor a HATEOAS specification. Both are only concepts, or architectural styles, if you will. _embedded is part of the HAL format.

它旨在嵌入(sic!)资源,否则将仅返回其URI.例如,假定GET http://localhost:8080/mywebservice/features仅返回URI列表,例如http://localhost:8080/mywebservice/features/GROUND,并且如果需要,您必须自己加载每个Feature.通过利用_embedded,所有Feature资源都将嵌入到响应中,因此您不必单独加载它们.

It's intended to embed (sic!) resources where otherwise only their URIs would be returned. For example GET http://localhost:8080/mywebservice/features is supposed to only return a list of URIs, like http://localhost:8080/mywebservice/features/GROUND, and you would have to load every single Feature yourself if you needed it. By utilizing _embedded all Feature resources get embedded into the response so you don't have to load them separately.

这篇关于"_embedded"的含义和用法.在HATEOAS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆