Spring HATEOAS with Spring Boot Tutorial
Introduction to Spring HATEOAS
HATEOAS, or Hypermedia as the Engine of Application State, is a constraint of the REST application architecture that distinguishes it from other network application architectures. Spring HATEOAS makes it easier to create RESTful APIs that are hypermedia-driven. This tutorial will guide you through the process of building a simple Spring Boot application with HATEOAS support.
Setting Up Your Spring Boot Project
To get started, you need to create a Spring Boot project. You can do this using Spring Initializr or your favorite IDE. For this example, we will use Spring Initializr.
1. Go to Spring Initializr.
2. Select the following options:
- Project: Maven Project
- Language: Java
- Spring Boot: 2.6.0 (or latest)
- Dependencies:
- Spring Web
- Spring HATEOAS
- Spring Data JPA
- H2 Database
3. Click on "Generate" to download your project.
Creating the Model
In this section, we will create a simple model class representing a "Book".
Create a class named Book.java in the model
package:
public class Book { private Long id; private String title; private String author; // Getters and Setters }
Creating the Repository
Next, we need to create a repository interface for our Book model.
Create a repository interface named BookRepository.java in the
repository
package:
import org.springframework.data.jpa.repository.JpaRepository; public interface BookRepository extends JpaRepository{ }
Creating the Controller
Now, let’s create a REST controller that will expose the endpoints for our Book entity.
Create a class named BookController.java in the controller
package:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.Link; import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/books") public class BookController { @Autowired private BookRepository bookRepository; @GetMapping public Listall() { return bookRepository.findAll(); } @GetMapping("/{id}") public EntityModel one(@PathVariable Long id) { Book book = bookRepository.findById(id) .orElseThrow(() -> new RuntimeException("Book not found")); EntityModel resource = EntityModel.of(book); Link selfLink = WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(BookController.class).one(id)).withSelfRel(); resource.add(selfLink); return resource; } @PostMapping public Book newBook(@RequestBody Book newBook) { return bookRepository.save(newBook); } }
Testing the Application
To test the application, run your Spring Boot application and use a tool like Postman or curl to interact with it. Here are some example commands to test the endpoints.
1. Get all books:
GET http://localhost:8080/books
2. Get a specific book:
GET http://localhost:8080/books/{id}
3. Add a new book:
POST http://localhost:8080/books Content-Type: application/json { "title": "Learning Spring HATEOAS", "author": "John Doe" }
Conclusion
In this tutorial, we explored the basics of Spring HATEOAS with Spring Boot. We created a simple REST API with hypermedia links, allowing clients to navigate the application state through the provided links. This is just the beginning; you can further enhance your application by adding more features and exploring the rich functionality that Spring HATEOAS offers.