Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning and usage of "_embedded" in HATEOAS

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

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

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?

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 }                }            },             ...         ]    } } 

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@[email protected]).

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.

like image 716
bluish Avatar asked Dec 10 '14 16:12

bluish


People also ask

What is _embedded in JSON?

resources. "_embedded": contains embedded resources. All other properties MUST be valid JSON, and represent the current state of the resource. The reserved "_links" property.

What is the use of HATEOAS in spring boot?

Spring HATEOAS provides some APIs to ease creating REST representations that follow the HATEOAS principle when working with Spring and especially Spring MVC. The core problem it tries to address is link creation and representation assembly.

What is HATEOAS example?

Example. A user-agent that implements HTTP makes a HTTP request of a REST API through a simple URL. All subsequent requests the user-agent may make are discovered inside the responses to each request. The media types used for these representations, and the link relations they may contain, are standardized.

What is Hal in HATEOAS?

HAL is a simple format that gives a consistent and easy way to hyperlink between resources in your API. The HAL format is strictly coupled to HATEOAS. The main target of HATEOAS is to decouple the API Consumer from the paths used in the API.


1 Answers

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.

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.

like image 109
a better oliver Avatar answered Sep 20 '22 10:09

a better oliver