HATEOAS Demo in Spring Boot Using HAL Browser(Simple REST Tutorial)

In this tutorial, you will learn how to create a simple HATEOAS application using Spring Boot.

HATEOAS stands for Hypermedia as the Engine of Application State. This is a feature that provides a way for response from the server provides additional information on what options are available next for the user to act on. A typical example is when you visit some web page, you will see additional links to other part of the website you can visit.

HAL on the other hand provides a consistent and easy format for you to hyperlink between resources in your API. HAL makes the API easily explorable and it’s documentation irrecoverable within the API.

 

The HAL Browser

The HAL browsers is application that is based on the HAL specification for handling HAL + JSON data. With this application, you can explore all the links in a Sprint Data REST API and use it to navigate through the resources. To use the HAL browser, you need to include the necessary dependency.

Let’s now do a demo in Spring Boot to demonstrate how the HAL browser works

 

Set up the Application in IntelliJ

Open IntelliJ and create a new application. Ensure to add the following dependencies:

  • H2 Database
  • Rest Repositories
  • Rest Repositories HAL Browser
  • Spring Web
  • Lombok
  • Jpa

 

Configure the H2 Database

I normally like to configure h2 database so I could manipulate the data using the UI Console. So to do this, open the application.properties file and include the following configuration

You take a few minutes to learn about H2 with Spring Boot

 

Create the Models

In this demo, we would only use two classes: Product and Category. There would be a ManyToOne relationship from the product to the Category.

The product class is given below

 

The category class is given below

 

Code the Repositories

As you know, a repository provides an interface to perform various operations involving Product and ProductCategory objects. But in this case, we would use Rest Repositories. This dependency dynamically create the url endpoints for the REST resources related with the objects in the application.

So you need to create the two repositories to extend JpaRepository.

Also annotate the repositories with the @RepositoryRestResource annotation(important)

 

Test the Application

At this point, you can go ahead to test the application. Remember to add the run configuration as described here.

Once the application launches and starts running on port 8080, open your browser and visit http://localhost:8080.

At this point, you will see the HAL browser window opens as shown below:

HAL Browser Window
HAL Browser Window

Now, you will see the links for the products and categories. You can use the buttons under GET and NON-GET columns to send a request to retrieve a list of products or categories. But at this time, there is no data in the database tables.

When you click on the NON-GET requests, you will have a form to enter records for the products or categories.

 

Add Some Data in H2 Database

I would also like you to get used to the H2 database. So visit http://localhost.h2-console. The H2 Console Login window opens as shown below:

H2 Console Window
H2 Console Login

 

Note the JDBC URL field, make sure it contains the name of the database as specified in the application.properties file.

Connect to your database by clicking on connect. This opens the actual H2 Console windows as shown below:

You can click on the any of the tables by the left to display the select statement. Then you can click on run to display the results in  a data sheet view. This provides an option to edit and add new records.

I beg to stop here. I have you’ve learnt about the HAL Browser, H2 Database, Rest Repositories etc. If you have any challenges please let me know in a comment box below. Also feel free to visit my channel on YouTube for video lessons and more!


Leave a Reply

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