Clean β’ Professional
HATEOAS (Hypermedia As The Engine Of Application State) is a key concept in REST APIs. It allows your API responses to include links that guide clients on what actions they can take next. This makes APIs more self-descriptive and easier to navigate.
Example:
{
"id":1,
"name":"John Doe",
"email":"[email protected]",
"_links":{
"self":{"href":"/api/users/1"},
"update":{"href":"/api/users/1"},
"delete":{"href":"/api/users/1"}
}
}
_links shows actions the client can take on the user resource.Add the Spring HATEOAS dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
User Entity:
publicclassUser {
private Long id;
private String name;
private String email;
// getters and setters
}
Controller with HATEOAS links:
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping("/api/users")
publicclassUserController {
privatestatic Map<Long, User> userRepo =newHashMap<>();
static {
userRepo.put(1L,newUser(1L,"John Doe","[email protected]"));
userRepo.put(2L,newUser(2L,"Jane Smith","[email protected]"));
}
@GetMapping("/{id}")
public EntityModel<User>getUserById(@PathVariable Long id) {
Useruser= userRepo.get(id);
EntityModel<User> resource = EntityModel.of(user);
// Add self link
resource.add(WebMvcLinkBuilder.linkTo(
WebMvcLinkBuilder.methodOn(UserController.class).getUserById(id)).withSelfRel());
// Add link to all users
resource.add(WebMvcLinkBuilder.linkTo(
WebMvcLinkBuilder.methodOn(UserController.class).getAllUsers()).withRel("all-users"));
return resource;
}
@GetMapping
public List<User>getAllUsers() {
returnnewArrayList<>(userRepo.values());
}
}
Explanation:
EntityModel<User> wraps the entity and allows adding links.WebMvcLinkBuilder.linkTo() creates links dynamically.self and all-users guide the client on next possible actions.import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
@GetMapping("/all")
public CollectionModel<EntityModel<User>>getAllUsersHateoas() {
List<EntityModel<User>> users =newArrayList<>();
for (User user : userRepo.values()) {
EntityModel<User> entityModel = EntityModel.of(user);
entityModel.add(WebMvcLinkBuilder.linkTo(
WebMvcLinkBuilder.methodOn(UserController.class).getUserById(user.getId())).withSelfRel());
users.add(entityModel);
}
return CollectionModel.of(users,
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(UserController.class).getAllUsersHateoas()).withSelfRel());
}
Key Points:
EntityModel to include links.CollectionModel to provide top-level links.self link β Every resource should point to itself.rel) β e.g., update, delete, all-users.next, prev links for large collections..withRel("custom-action")Page to add links for paginationHATEOAS (Hypermedia As The Engine Of Application State) is an extension of REST that adds navigational links to API responses, making APIs easier to use and evolve. Here's a clear comparison:
| Feature | Normal REST | HATEOAS REST |
|---|---|---|
| Links to actions | None | Links included in response for related actions |
| Client knowledge | Client must know all URLs | Client can discover URLs dynamically through links |
| Ease of evolution | Hard to change endpoints without breaking clients | Easier to evolve without breaking clients |
| Self-documenting | No | API responses guide the client with links |
HATEOAS in Spring Boot:
EntityModel and CollectionModel to wrap responses.