Enterprise Java

Choosing Between Resource, EntityModel & RepresentationModel in Spring HATEOAS

When building hypermedia-driven REST APIs using Spring HATEOAS, a critical decision is how to model your API responses — especially how to wrap your domain data alongside hypermedia links. Spring HATEOAS provides several wrapper classes, mainly Resource (now deprecated), EntityModel, and RepresentationModel. Understanding these and related types like CollectionModel and PagedModel helps you design clean, maintainable, and self-descriptive APIs.

What Are These Types?

1. Resource (Deprecated)

  • What it was: A generic wrapper combining a domain object and hypermedia links.
  • Why deprecated: The Spring team replaced it with clearer abstractions for better flexibility and clarity.
  • Advice: Avoid using it in new applications.

2. EntityModel<T>

EntityModel<T> wraps a single domain entity and allows attaching hypermedia links.

@Entity
public class Book {
    @Id private Long id;
    private String title;
    // getters and setters
}

Example usage in a controller method:

@GetMapping("/books/{id}")
public EntityModel<Book> getBook(@PathVariable Long id) {
    Book book = bookService.findById(id);

    return EntityModel.of(book,
        linkTo(methodOn(BookController.class).getBook(id)).withSelfRel(),
        linkTo(methodOn(BookController.class).getAllBooks()).withRel("books"));
}

Here, the Book entity is wrapped inside EntityModel, and two links are added — one self link, and one to the list of all books.

3. RepresentationModel

RepresentationModel is a base class for hypermedia-driven representations that do not contain a domain entity.

Example:

@GetMapping("/api")
public RepresentationModel<?> root() {
    RepresentationModel<?> model = new RepresentationModel<>();
    model.add(linkTo(methodOn(BookController.class).getAllBooks()).withRel("books"));
    model.add(linkTo(methodOn(AuthorController.class).getAllAuthors()).withRel("authors"));
    return model;
}

Use RepresentationModel for root endpoints, status resources, or any resource where you want to return only links or metadata without embedding an entity.

Handling Collections of Resources

For collections of resources, Spring HATEOAS provides:

CollectionModel<T>

Wraps a collection of items, typically a collection of EntityModel<T>.

Example:

@GetMapping("/books")
public CollectionModel<EntityModel<Book>> getAllBooks() {
    List<EntityModel<Book>> books = bookService.findAll().stream()
        .map(book -> EntityModel.of(book,
            linkTo(methodOn(BookController.class).getBook(book.getId())).withSelfRel()))
        .collect(Collectors.toList());

    return CollectionModel.of(books,
        linkTo(methodOn(BookController.class).getAllBooks()).withSelfRel());
}

This wraps each Book in an EntityModel, and the whole list is wrapped in a CollectionModel with a self link.

PagedModel<T>

When you want to support pagination, Spring HATEOAS integrates with Spring Data’s Page<T> type.

Example:

@GetMapping("/books")
public PagedModel<EntityModel<Book>> getBooks(Pageable pageable, PagedResourcesAssembler<Book> assembler) {
    Page<Book> page = bookService.findAll(pageable);
    return assembler.toModel(page, book -> EntityModel.of(book,
        linkTo(methodOn(BookController.class).getBook(book.getId())).withSelfRel()));
}

Here:

  • PagedResourcesAssembler automatically builds pagination links (next, prev, first, last).
  • Each item is wrapped in an EntityModel.

Summary Table

TypeDescriptionUse Case
Resource<T>Deprecated wrapper for entity + linksAvoid in new code
EntityModel<T>Wraps a single entity + linksSingle resource with embedded data
RepresentationModelBase class for links-only or metadata responsesRoot API, status, or pure links
CollectionModel<T>Collection of EntityModel or RepresentationModelLists of resources
PagedModel<T>CollectionModel + pagination metadata and linksPaginated REST collections

Useful Resources

Final Thoughts

Choosing the right model helps keep your API consistent and intuitive:

  • Use EntityModel for domain objects with links.
  • Use RepresentationModel when only links or metadata are needed.
  • Use CollectionModel and PagedModel for lists and paginated data.

Mastering these types makes your REST APIs truly RESTful — discoverable, navigable, and self-descriptive.

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button