How to start Spring HATEOAS RestAPI with Spring Boot

Spring HATEOAS project helps to create REST representations that follow HATEOAS (Hypertext as the Engine of Application State) principle. In this article, we’re gonna look at way to build a Rest Service using Spring HATEOAS with Spring Boot.

I. Overview


In general, HATEOAS principle implies that with each response, API should shows Clients appropriate information about the next potential steps or guide to deeper understanding itself.

For example, if client requests for specific Customer information:
The Service should provide link to get all Orders of that Customer in the response:

2. Spring HATEOAS

Spring HATEOAS provides a set of useful types to ease working with those.

Firstly, we need create a Link object:
– Way 1: using Spring HATEOAS ControllerLinkBuilder:

– Way 2: using Spring HATEOAS EntityLinks:

Then, add Link object to ResourceSupport object:
– Way 1: extending ResourceSupport class

– Way 2: using Resources (that extends ResourceSupport class)

Finally, just return YourClass object or Resource object to Client.

II. Practice

1. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE

2. Project Overview


Controller -> Repository -> Model:
+ CustomerController (@ExposesResourceFor(...)) -> CustomerRepository -> Customer(id, name, orders)
+ PersonController -> PersonRepository -> Person(id, name, orders) extends ResourceSupport

Dependency for Spring HATEOAS in pom.xml.

3. Step by step
3.1 Create Spring Boot project

Using Spring Tool Suite/Eclipse to create Project and add Dependencies to pom.xml file:

3.2 Create Data Model Classes

3.3 Create Repository Classes

3.4 Create Controller Classes

3.5 Run & Check Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results by open your browser and send request:

Request 1:

Request 2:

Request 3:

Request 4:

III. Source Code


By grokonez | June 9, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *