Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

The future of AI-driven development. Join the discussion around insights on low code's and AI's roles in building mission-critical apps.

Has GenAI transformed how you work? Or has the hype cycle played out? Tell us your thoughts on its impact inn your organization.

DevEx Roundtable: Join the discussion about the evolving needs of the developer from streamlined workflows to infrastructures investments.

Spring Boot - HATEOAS for RESTful Services

In this post, we take a look at how you can implement a HATEOAS-based RESTful service using Spring Boot. If this sounds interesting, read on for the details!

By  · Tutorial
Comment (0)
Save
23.9K Views

This guide will help you implement HATEOAS for a REST API/Service with Spring Boot.

You Will Learn

  • What is HATEOAS?
  • Why do you need HATEOAS?
  • How do you implement HATEOAS with Spring Boot?
  • What are the HATEOAS best practices?

10 Step Reference Courses

Project Code Structure

Following screenshot shows the structure of the project we will create.Image

A few details:

  • SpringBoot2RestServiceApplication.java - The Spring Boot Application class generated with Spring Initializer. This class acts as the launching point for the application.
  • pom.xml - Contains all the dependencies needed to build this project. We will use Spring Boot Starter AOP.
  • Student.java - Student JPA Entity
  • StudentRepository.java - Student JPA Repository. This is created using Spring Data JpaRepository.
  • StudentResource.java - Spring Rest Controller exposing all services on the student resource.
  • data.sql - Initial data for the student table. Spring Boot would execute this script after the tables are created from the entities.
  • Maven 3.0+ is your build tool
  • Your favorite IDE. We use Eclipse.
  • JDK 1.8+

Complete Maven Project With Code Examples

Our Github repository has all the code examples - https://github.com/in28minutes/spring-boot-examples/tree/master/spring-boot-2-rest-service-hateoas

Richardson Maturity Model

Richardson Maturity Model defines the maturity level of a Restful Web Service. Following are the different levels and their characteristics.

  • Level 0: Expose SOAP web services in REST style. Expose action based services (http://server/getPosts, http://server/deletePosts, http://server/doThis, http://server/doThat etc) using REST.
  • Level 1: Expose Resources with proper URI’s (using nouns). Ex: http://server/accounts, http://server/accounts/10. However, HTTP Methods are not used.
  • Level 2: Resources use proper URI’s + HTTP Methods. For example, to update an account, you do a PUT. To create an account, you do a POST. Uri’s look like posts/1/comments/5 and accounts/1/friends/1.
  • Level 3: HATEOAS (Hypermedia as the engine of application state). You will tell not only about the information being requested but also about the next possible actions that the service consumer can do. When requesting information about a Facebook user, a REST service can return user details along with information about how to get his recent posts, how to get his recent comments and how to retrieve his friend’s list.

What Is HATEOAS?

HATEOAS stands for “Hypermedia as the engine of application state.” It's a complicated acronym. Let’s decode it for you.

What do you see when you visit a web page?

The data that you would want to see. Is that all? You would also see links and buttons to see related data.

For example, if you go to a student page, you will see

  • Student profile
  • Links to Edit and Delete Student details
  • Links to see details of other students
  • Link to see details of the courses and grades of the student

HATEOAS brings the same concepts to RESTful Web Services.

When some details of a resource are requested, you will provide the resource details as well as details of related resources and the possible actions you can perform on the resource. For example, when requesting information about a Facebook user, a REST service can return the following

  • User details
  • Links to get his recent posts
  • Links to get his recent comments
  • Links to retrieve his friend’s list.

Bootstrapping a Project With a REST Resource

In the previous article in the series, we set up a simple RESTful service with a resource exposing CRUD methods.

We will use the same example to discuss HATEOAS.

Implementing HATEOAS With Spring Boot

Spring Boot provides a Starter for HATEOAS. Include the dependency in your pom.xml


Components in Spring Boot HATEOAS Starter

Listed below are some of the important dependencies from spring-boot-starter-hateoas.



The most important dependency is spring-hateoas.

Enhancing the Resource to Return HATEOAS Response

To implement HATEOAS, we would need to include related resources in the response.

Instead of Student, we use a return type of Resource<Student>. Resource is a simple class wrapping a domain object and allows adding links to it.


We create a new resource.


We add the link to retrieve all students method to the links.


The response when we execute a GET request to http://localhost:8080/students/10001 is shown below:


You can see that there is a new section _links with a link to all students Resource.

Enhance Other Resources With HATEOAS

Above example covers important concepts in enhancing resources with HATEOAS.

However, you have to make the important decision:

  • What are the important resources related to a specific resource?

Go ahead and enhance the application with more HATEOAS links.

Next Steps

Complete Code Example

/pom.xml



/src/main/java/com/in28minutes/springboot/rest/example/SpringBoot2RestServiceApplication.java



/src/main/java/com/in28minutes/springboot/rest/example/student/Student.java



/src/main/java/com/in28minutes/springboot/rest/example/student/StudentNotFoundException.java



/src/main/java/com/in28minutes/springboot/rest/example/student/StudentRepository.java



/src/main/java/com/in28minutes/springboot/rest/example/student/StudentResource.java



/src/main/resources/data.sql



/src/test/java/com/in28minutes/springboot/rest/example/SpringBoot2RestServiceApplicationTests.java



Published at DZone with permission of Ranga Rao Karanam. See the original article here.

Opinions expressed by DZone contributors are their own.


Comments