I'm trying to implement a HATEOAS Rest Client using Spring Boot.
Right now, I'm stuck in a point where I need to convert HATEOAS into an actual API URI.
If I post a new object of type Customer like:
{
"name": "Frank",
"address": "http://localhost:8080/address/23"
}
And then I retrieved with a request to http://localhost:8080/api/customer/1`, HATEOAS gives me something like
{
"name": Frank,
"_links": {
"address": {
"href": "http://localhost:8080/api/customer/1/address"
}
}
}
Is it possible to convert a link of the form of http://localhost:8080/api/customer/1/address to an API call like http://localhost:8080/api/address/23 ?
If you see what HATEOS returns after you say,
GET: http://localhost:8080/api/customer/1is
According to Understanding HATEOS,
which means,
after you have received resource details with
http://localhost:8080/api/customer/1what 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/addressthat was accessible once you havecustomer/1and from there if you want then without going anywhere elsecustomer/1's address could be found with/customer/1/address.Similarly if you have
/customer/1'soccupationdetails then there would be another link belowaddresslink calledhttp://localhost:8080/api/customer/1/occupation.So if
addressis dependent oncustomeri.e. there can be noaddresswithoutcustomerthen your API endpoint has to be/api/customer/1/addressand 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
LinkBuilderinterface of HATEOS.Example: With object of type
Customerlike: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 !