Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is HATEOAS, and how does Spring HATEOAS help you add links to a response for GET /orders/{id}?

HATEOAS, hypermedia as the engine of application state, means a response carries links that tell the client what it can do next, so the client follows links instead of building URLs. Spring HATEOAS gives us RepresentationModel, EntityModel and CollectionModel to carry links, and WebMvcLinkBuilder.linkTo(methodOn(OrderController.class).get(id)).withSelfRel() builds a link from a controller mapping. By default the result is rendered as HAL with _links and _embedded, and adding links only when an action is allowed, such as cancel, exposes the order's state transitions.

Why this question is asked

HATEOAS is the part of REST that most APIs skip, and also the part interviewers like to ask about, because it tests whether a candidate knows REST beyond URLs and HTTP verbs. Spring makes it easy to try, so it is also a practical question. In this answer, we will explain what hypermedia means with a concrete order, look at the model classes Spring HATEOAS provides, build links from controller methods, see the HAL output, add a link that depends on the order's state, and reuse the logic with an assembler.

1What Hypermedia Means in Plain Words

A web page is hypermedia: it contains links and forms, and we use the site by following them, not by typing URLs we memorised. HATEOAS asks APIs to behave the same way. When a client fetches an order, the response includes not only the order's data but also links: where to find this order again (self), where its customer is, and which actions are possible now, such as cancel or pay.

The application state in the name is the client's position in a workflow. The links in each response are the engine that moves it forward: the client learns from the server what the next steps are. In theory, a client that follows links needs to know only the entry point and the meaning of each link relation (the name of the link, such as cancel), and the server can change URLs freely.

2The Model Classes of Spring HATEOAS

Spring HATEOAS gives us wrapper types that hold links next to our data. RepresentationModel is the base class: anything that extends it has a list of links. EntityModel<T> wraps a single object, such as one OrderDto, and adds links to it. CollectionModel<T> wraps a list of items with links for the whole collection, and PagedModel adds paging information and next and previous links. We add the dependency spring-boot-starter-hateoas, and Spring Boot configures the rest.

3Building Links From Controller Methods

Writing URLs by hand would defeat the purpose, because they would break when a mapping changes. WebMvcLinkBuilder builds links from our controller mappings instead. methodOn creates a recording proxy of the controller: calling get(id) on it does not run the method, it only records which method and arguments we meant, and linkTo turns that into a URL.

Java
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
@RestController
@RequestMapping("/orders")
class OrderController {
@GetMapping("/{id}")
EntityModel<OrderDto> get(@PathVariable long id) {
Order order = service.find(id);
EntityModel<OrderDto> model = EntityModel.of(OrderDto.from(order),
linkTo(methodOn(OrderController.class).get(id)).withSelfRel(), // /orders/17
linkTo(OrderController.class).withRel("orders")); // /orders
if (order.isCancellable()) {
model.add(linkTo(methodOn(OrderController.class).cancel(id)).withRel("cancel"));
}
return model;
}
@PostMapping("/{id}/cancel")
ResponseEntity<Void> cancel(@PathVariable long id) { service.cancel(id); return ResponseEntity.noContent().build(); }
}

Notice the if statement. The cancel link is added only when the order can actually be cancelled. This is where hypermedia becomes useful: the client does not need to know the cancellation rules, it just shows a cancel button when the link is present.

4What the Response Looks Like

Spring HATEOAS renders links in HAL, the Hypertext Application Language, by default. HAL puts links under a _links object, keyed by relation name, and embedded resources under _embedded.

Java
GET /orders/17 (order is PAID, still cancellable)
Content-Type: application/hal+json
{
"id": 17, "status": "PAID", "total": "39.90",
"_links": {
"self": { "href": "http://localhost:8080/orders/17" },
"orders": { "href": "http://localhost:8080/orders" },
"cancel": { "href": "http://localhost:8080/orders/17/cancel" }
}
}
GET /orders/18 (order is SHIPPED)
{ "id": 18, "status": "SHIPPED", "total": "12.00",
"_links": { "self": { "href": "http://localhost:8080/orders/18" },
"orders": { "href": "http://localhost:8080/orders" } } } // no cancel link
Step throughWhich links an order gets
  1. Build EntityModel with order dataOrderDto.from(order)
  2. Add self and orders linksalways present
  3. Is the order cancellable?
    YesAdd cancel linkNoLeave it out
  4. Render as HAL_links keyed by relation

4 steps in order. Play them, or step through them one at a time.

The links are absolute URLs built from the current request, so they respect the host and port the client used; behind a proxy, Spring respects forwarded headers when configured to do so. Other media types, such as HAL-FORMS, Collection+JSON or UBER, can be enabled when clients need them.

5Reusing the Logic With an Assembler

The same order appears in several responses: alone, in a list, embedded in a customer. To avoid repeating the link code, Spring HATEOAS offers RepresentationModelAssembler, a small component that turns a domain object into its model with links.

Java
@Component
class OrderModelAssembler implements RepresentationModelAssembler<Order, EntityModel<OrderDto>> {
@Override
public EntityModel<OrderDto> toModel(Order order) {
EntityModel<OrderDto> m = EntityModel.of(OrderDto.from(order),
linkTo(methodOn(OrderController.class).get(order.id())).withSelfRel());
if (order.isCancellable()) m.add(linkTo(methodOn(OrderController.class).cancel(order.id())).withRel("cancel"));
return m;
}
}
// assembler.toCollectionModel(orders) -> CollectionModel with _embedded.orderDtoList

Now the link rules live in one place, and every response that contains an order shows the same links.

6Follow-up questions

When does HATEOAS add value and when is it unused overhead?

It adds value when clients actually follow links: for paging with next and previous links, for actions that depend on state such as cancel or pay, for generic clients and API browsers, and for workflows whose rules change while clients should not. It is overhead when every client hardcodes URLs from documentation and ignores _links, because then we pay for larger payloads, an extra library and learning time for no benefit. Many teams settle on self and paging links plus state-dependent actions, without a full hypermedia client.

Why use methodOn instead of writing the path?

Because the link then follows the mapping. If someone changes @RequestMapping("/orders") to /purchase-orders, every link updates automatically, while handwritten strings would silently point to the old URL.

Is a REST API without hypermedia really REST?

Strictly, by Roy Fielding's definition, hypermedia is one of the constraints, so an API without it is not fully RESTful. In practice the term is used loosely for resource-oriented HTTP APIs, and most successful public APIs do not use full HATEOAS.

7Conclusion

In this answer, we saw that HATEOAS means responses carry links that tell clients what they can do next. Spring HATEOAS provides RepresentationModel, EntityModel and CollectionModel to hold links, WebMvcLinkBuilder builds them from controller mappings with linkTo and methodOn, and HAL renders them under _links. Adding a link only when an action is allowed, such as cancel, exposes the state transitions of the resource, and an assembler keeps the link rules in one place.

What to say in the interviewAbout 80 seconds aloud

Open with

HATEOAS stands for hypermedia as the engine of application state. The idea is that a response contains not only data but also links that tell the client what it can do next, like a web page with links, so the client follows links by their relation name instead of building URLs itself.

Then, if they want more

In Spring, I add spring-boot-starter-hateoas and wrap my DTOs in its model types: EntityModel for one resource, CollectionModel for lists and PagedModel for pages, all based on RepresentationModel, which holds the links.

For GET /orders/{id}, I would return EntityModel.of the order DTO with linkTo methodOn OrderController get id withSelfRel, plus a link to the orders collection. methodOn records the controller call, so the link follows the mapping if the path changes.

The interesting part is conditional links: I add a cancel link only when the order is cancellable, so the client shows a cancel button when the link is there and does not need to know the rules.

By default Spring renders this as HAL, with links under _links and embedded resources under _embedded, and other media types such as HAL-FORMS can be enabled. To reuse the link logic across responses, I would put it in a RepresentationModelAssembler.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is HATEOAS, and how does Spring HATEOAS help you add links to a response for GET...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/fa22771b8f/