Imagine a REST endpoint (/employees)
serving pages of employees in JSON HAL format.
An employee lives in a country, which resides in a continent.
For both countries and continents there are also separate endpoints.
The returned pages contain the typical _embedded
field with the employee data.
The employee resource also contains the nested country
resource.
This nested country
resource also contains the _links
.
In this case the output would be:
GET /employees
{
"_embedded": {
"employees": [{
"employee_id": 1
"name": "Mr. X",
"place_name": "London",
"country": {
"alpha2_code": "AU",
"name": "Australia",
"continent": {
"code": "OC",
"name": "Australia",
"_links": {
"self": {
"href": "http://localhost:8077/continents/au"
}
}
},
"_links": {
"self": {
"href": "http://localhost:8077/countries/au"
}
}
},
"_links": {
"self": {
"href": "http://localhost:8077/employees/1"
}
}
},
{
..
}
]
},
"_links": {
"first": {
"href": "http://localhost:8077/employees?page=1&size=10"
},
"self": {
"href": "http://localhost:8077/employees"
},
"next": {
"href": "http://localhost:8077/employees?page=2&size=10"
},
"last": {
"href": "http://localhost:8077/employees?page=8&size=10"
}
},
"page": {
"size": 10,
"total_elements": 71,
"total_pages": 8,
"number": 0
}
}
Is the nesting of the country
(and also the nesting of continent
within the country
outputted in the correct way following the HAL specification.
In some other examples on the I noticed the following format:
{
"_embedded": {
"employees": [{
"employee_id": 1
"name": "Mr. X",
"place_name": "London",
"_embedded": {
"country": {
"alpha2_code": "AU",
"name": "Australia",
"_embedded": {
"continent": {
"code": "OC",
"name": "Australia",
"_links": {
"self": {
"href": "http://localhost:8077/continents/au"
}
}
},
}
"_links": {
"self": {
"href": "http://localhost:8077/countries/au"
}
}
}
},
"_links": {
"self": {
"href": "http://localhost:8077/employees/1"
}
}
},
{
..
}
]
},
"_links": {
"first": {
"href": "http://localhost:8077/employees?page=1&size=10"
},
"self": {
"href": "http://localhost:8077/employees"
},
"next": {
"href": "http://localhost:8077/employees?page=2&size=10"
},
"last": {
"href": "http://localhost:8077/employees?page=8&size=10"
}
},
"page": {
"size": 10,
"total_elements": 71,
"total_pages": 8,
"number": 0
}
}
UPDATED: second example now also clearly shows it is a paged response.
It uses nested _embedded
resources.
Is there - in perspective of the specification - one approach better then the other? Or are the both valid?