EntityModel is a Spring HATEOAS class that wraps a domain object (the content) together with a list of hypermedia links. When a controller returns an EntityModel, the JSON response contains the object's fields plus a _links section (in HAL format) that tells clients which related resources and actions are available.
It extends RepresentationModel, which provides link handling, and adds the wrapped content. You normally create instances with EntityModel.of(...) rather than subclassing it.
HATEOAS (Hypermedia as the Engine of Application State) is the REST constraint that responses carry links for the next possible steps. Spring HATEOAS offers a small set of model types:
| Type | Use |
|---|---|
RepresentationModel | Base type with links only; extend it for custom DTOs |
EntityModel<T> | One resource: content of type T plus links |
CollectionModel<T> | A collection of items plus links for the collection |
PagedModel<T> | A collection with page metadata (size, number, total) |
EntityModel serialises the wrapped object unwrapped, so the JSON looks like the plain resource with an added _links property.
With Spring Boot 3, add the starter and Boot configures HAL support automatically.
Spring Boot 3 uses Jakarta namespaces (jakarta.persistence), not javax.persistence as in older examples.
Build links with WebMvcLinkBuilder, using linkTo and methodOn so URLs follow your controller mappings and do not drift when paths change.
A request to GET /api/books/1 returns application/hal+json:
The self link identifies the resource itself, and books points to the collection. The link names (relations) are what clients rely on, so keep them stable.
Repeating linkTo calls in every method is error-prone. Implement RepresentationModelAssembler once and reuse it, which keeps links consistent between single-item and list responses.
In the controller, call assembler.toModel(book) for one item and assembler.toCollectionModel(books) for a list. Add the same static imports for linkTo and methodOn as before.
cancel link on an order only while it can still be cancelled.withRel names from the IANA link relations registry where one fits, such as self, next, prev, collection.EntityModel for each type. Return EntityModel<Book> directly; extend RepresentationModel only when you need a custom shape.RepresentationModel holds only links. EntityModel<T> extends it and also holds a content object of type T, whose properties are serialised at the top level of the JSON.
By default Spring Boot configures HAL, so responses use application/hal+json. Other formats, such as HAL-FORMS, can be enabled through Spring HATEOAS configuration.
Strictly, hypermedia is part of the REST architectural style, but many production APIs return plain JSON. Use EntityModel when clients benefit from discoverable links.
It works, but a DTO is safer. It avoids leaking schema details and lazy-loading issues, and you can change the database without changing the API.
EntityModel pairs a resource with its hypermedia links so that Spring HATEOAS can render HAL responses with a _links section. Create it with EntityModel.of, build links with linkTo and methodOn, centralise the logic in a RepresentationModelAssembler, and prefer DTOs over raw entities. On Spring Boot 3, add spring-boot-starter-hateoas and use jakarta imports.