2
votes

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 ?

1
does http://localhost:8080/api/customer/1/address and http://localhost:8080/api/address/23 get you the same resources ? After looking at the API endpoints it doesn't seem like that - rdj7
@rdj7 yea, http://localhost:8080/api/customer/1/address' is the way HATEOAS points you to localhost:8080/api/address/23` - Alberto Castaño
ok, I think question is little unclear to me, when you do GET request for http://localhost:8080/api/customer/1/address then in the response under the links part you want to give http://localhost:8080/api/address/23 link - rdj7
@rjd7 I've updated the question. - Alberto Castaño
Spring HATEOAS doesn't add links. What other library do you use? Spring Data REST? And how does the Customer class look like? - a better oliver

1 Answers

0
votes

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 !