julien carax
julien carax

Reputation: 335

How to achieve spring Hateoas in the current Rest API?

I have successfully implemented RestFul API as per the standards but have not incorporated all of the features in my API. For instance, my API currently adheres to the URI standard and the proper verb usage as per the explained standards. What I want to achieve is an API with level 3 implementation according to this article

http://zeroturnaround.com/rebellabs/beyond-rest-how-to-build-a-hateoas-api-in-java-with-spring-mvc-jersey-jax-rs-and-vraptor/

That means I am trying to implement HATEOAS in my restful API. I have started reading the articles to understand about it and have got a blurry vision to get what I want. What I need is to have a basic plan as to what steps are important for HATEOAS ?

Alright, I have a Rest API which sends the request postcode to a service

http://localhost:8080/api/india/{postcode}

Here postcode being the path parameter.

When called, it responds back with all the possible address matches for that particular postcode. Now, the API i have made is giving the proper response in both xml and json format and I have no complains. Now, as the next step I need to implement the feature HATEOAS in this API. I have got the basic understanding of HATEOAS but how would it work for my API is my query.

Upvotes: 1

Views: 420

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 82008

The idea behind HATEOAS is that you need no up front knowledge of an API except an initial URL.

In your case this might be the url:

http://localhost:8080/api/

The resource on that URL might contain a list of links for various countries, leading to

http://localhost:8080/api/india
http://localhost:8080/api/china
http://localhost:8080/api/australia

and so on. In those resources there might be further links or a form or both leading to

http://localhost:8080/api/india/{postcode}

Of course you might have a form on the first page, leading directly to the final URL and many variation more.

The result of this design is, that your application can change the urls at any time, without breaking anything.

One important implementation detail is that of 'rel' for communicating the purpose of the link. The interpretation of that purpose actually has to go into the implementation of the client, which is a bummer, but I guess until there is a really strong AI implemented in all http clients, this is the only choice we have.

Upvotes: 1

Related Questions