If you see what HATEOS returns after you say,
GET: http://localhost:8080/api/customer/1
is
{
"name": Frank,
"_links": {
"address": {
"href": "http://localhost:8080/api/customer/1/address"
}
}
}
According to Understanding HATEOS,
It's possible to build more complex relationships. With HATEOAS, the output makes it
easy to glean how to interact with the service without looking up a specification or
other external document
which means,
after you have received resource details with
http://localhost:8080/api/customer/1
what other operations are possible with the received resource those will be shown for easier/click thru access to your service/application,
here in this case HATEOS could find a link http://localhost:8080/api/customer/1/address
that was accessible once you have customer/1
and from there if you want then without going anywhere else customer/1
's address could be found with /customer/1/address
.
Similarly if you have /customer/1
's occupation
details then there would be another link below address
link called http://localhost:8080/api/customer/1/occupation
.
So if address
is dependent on customer
i.e. there can be no address
without customer
then your API endpoint has to be /api/customer/1/address
and not directly /api/address/23
.
However, after understanding these standards and logic behind HATEOS's such responses if you still want to go with your own links that may not align with HATEOS's logic you can use,
Link Object provided by LinkBuilder
interface of HATEOS.
Example:
With object of type Customer
like:
Customer c = new Customer( /*parameters*/ );
Link link= linkTo(AnyController.class).slash("address").slash(addressId);
customer.add(link);
//considering you want to add link `http://localhost:8080/api/address/23` and `23 is your addressID`.
Also you can create a list of Links and keep adding many such links to that list and then add that list to your object.
Hope this helps you !
http://localhost:8080/api/customer/1/address
andhttp://localhost:8080/api/address/23
get you the same resources ? After looking at the API endpoints it doesn't seem like that - rdj7http://localhost:8080/api/customer/1/address' is the way HATEOAS points you to
localhost:8080/api/address/23` - Alberto Castañohttp://localhost:8080/api/customer/1/address
then in the response under thelinks
part you want to givehttp://localhost:8080/api/address/23
link - rdj7Customer
class look like? - a better oliver