Skip to main content

Spring Data REST and HAL

Normally, linking your Spring Data JPA repositories to a REST API requires building @RestController classes and manually mapping @GetMapping to the repository functions.

Spring Data REST

By simply adding the spring-boot-starter-data-rest dependency, Spring Boot evaluates your generic JpaRepository interfaces and automatically projects a fully working C.R.U.D. REST API over them.

If you have a UserRepository, Spring Data REST instantly exposes:

  • GET /users (Read All)
  • POST /users (Create)
  • GET /users/{id} (Read Single)
  • PUT /users/{id} (Update Single)
  • DELETE /users/{id} (Delete)

Hypertext Application Language (HAL)

When Spring Data REST returns JSON, it formats the payload using the HAL specification. HAL introduces a _links object inside the JSON payload. This provides structural API navigation natively. A client requesting a specific user will also receive explicit REST URLs detailing exactly how to delete or modify that user.

{
"firstName": "Alice",
"lastName": "Smith",
"_links": {
"self": {
"href": "http://localhost:8080/users/1"
},
"user": {
"href": "http://localhost:8080/users/1"
}
}
}