Quick revision
The short answer
We return an EntityModel<OrderDto> built with EntityModel.of(dto, linkTo(methodOn(OrderController.class).get(id)).withSelfRel()) and add the cancel link only when order.isCancellable() is true. In HAL, the links appear under _links next to the order's fields, so a cancellable order has self and cancel links and a shipped one has only self. Link building belongs in a RepresentationModelAssembler, and lists use CollectionModel or PagedModel.
What to remember
In this answer, we wrote a Spring HATEOAS controller that returns an EntityModel<OrderDto> with a self link built by linkTo(methodOn(...)) and a cancel link added only when the order is cancellable. In HAL, links appear under _links next to the order's fields, so clients can show actions based on link presence. We moved link building into a RepresentationModelAssembler and used CollectionModel and PagedModel with PagedResourcesAssembler for lists.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We return an EntityModel<OrderDto> built with EntityModel.of(dto, linkTo(methodOn(OrderController.class).get(id)).withSelfRel()) and add the cancel link only when order.isCancellable() is true. In HAL, the links appear under _links next to the order's fields, so a cancellable order has self and cancel links and a shipped one has only self. Link building belongs in a RepresentationModelAssembler, and lists use CollectionModel or PagedModel.
Why this question is asked
Most REST APIs return data and leave clients to work out what they may do next. The client then copies the server's rules, for example that an order can be cancelled only before it ships, and the two copies drift apart. Hypermedia puts those decisions in the response as links. Interviewers ask this coding question to see whether we can use Spring HATEOAS's link builders correctly and explain what the client gains. In this answer, we will recap HAL and the main Spring HATEOAS types, write the controller with a conditional link, look at the response in both states, move link building into an assembler, and handle lists.
1A Quick Recap of HAL and Spring HATEOAS
HAL, Hypertext Application Language, is a simple JSON convention: a resource's own fields stay at the top level, links go into a _links object keyed by relation name, and embedded resources go into _embedded. A relation name, or rel, says what the link means: self is the resource itself, and cancel is a name we choose for the cancel action.
Spring HATEOAS provides the model types and builders. EntityModel<T> wraps one object and its links, CollectionModel<T> wraps a list, and PagedModel<T> adds page information. WebMvcLinkBuilder builds links from controller methods, so URLs are never hard-coded. With spring-boot-starter-hateoas, Boot configures HAL rendering, and responses use the application/hal+json media type.
2Writing the Controller
Here is the controller. We build the self link from the get method and the cancel link from the cancel method, using methodOn, which records a call on a proxy of the controller so that linkTo can read the method's mapping and arguments.
Notice that methodOn(OrderController.class).get(id) does not call the real method. It returns a recording proxy; linkTo then turns the recorded call into /orders/42 using the @RequestMapping and @GetMapping paths. If someone later changes the path in the mapping, the links follow automatically. The rule that decides the link, isCancellable(), lives on the server, in one place.
3What the Response Looks Like in HAL
For an order that has not shipped, the response has both links; for a shipped order, only the self link.
| Status | isCancellable() | Links in _links |
|---|---|---|
| PLACED | true | self, cancel |
| PAID | true | self, cancel |
| SHIPPED | false | self |
| CANCELLED | false | self |
So the client's logic becomes simple: show the Cancel button if the response has a cancel link, and follow its href when the user clicks. When the business changes the rule, for example allowing cancellation until the parcel leaves the warehouse, only the server changes. Clients can drive the UI from link presence instead of duplicating state rules.
4Moving Link Building Into an Assembler
Building links in every controller method repeats code, and the same order may be rendered from several endpoints. A RepresentationModelAssembler converts a domain object into a model with its links in one place.
5Returning Lists With CollectionModel and PagedModel
For lists, we wrap the models in a CollectionModel with a self link for the list. For pages, PagedResourcesAssembler, which Spring Data's web support provides, turns a Page into a PagedModel with first, prev, next and last links and the page metadata.
The name of the embedded list comes from the DTO type; without help it would be orderDtoList. Annotating OrderDto with @Relation(collectionRelation = "orders") gives the list a clean name.
6Follow-up questions
How does linkTo work behind a reverse proxy that changes the host?
linkTo builds absolute URLs from the current request. Behind a proxy or load balancer, the application sees the internal host, port and scheme, so links would point to something like http://orders-7f9c:8080. The proxy sends X-Forwarded-Host, X-Forwarded-Proto and X-Forwarded-Port or the standard Forwarded header, and we tell Spring to use them with server.forward-headers-strategy=framework, which installs Spring's ForwardedHeaderFilter, or native to let the embedded server handle them. Only trust these headers when the application is reachable through the proxy alone, because a client could otherwise forge them.
Why use a link instead of a boolean cancellable field?
A link tells the client both that the action is allowed and where to perform it, so the client does not build URLs. It also extends naturally: new actions appear as new links without new fields, and old clients ignore rels they do not know.
Does methodOn work with any controller?
It needs to create a proxy of the controller class and of the method's return type, so the controller must not be final and the return types must be proxyable, such as EntityModel or ResponseEntity. If that is a problem, linkTo(OrderController.class).slash(id) builds the link from the class mapping instead.
7Conclusion
In this answer, we wrote a Spring HATEOAS controller that returns an EntityModel<OrderDto> with a self link built by linkTo(methodOn(...)) and a cancel link added only when the order is cancellable. In HAL, links appear under _links next to the order's fields, so clients can show actions based on link presence. We moved link building into a RepresentationModelAssembler and used CollectionModel and PagedModel with PagedResourcesAssembler for lists.
Open with
I would return an EntityModel of OrderDto from the controller. I build it with EntityModel.of, passing the DTO and a self link made with linkTo of methodOn OrderController get id, withSelfRel. methodOn records the call on a proxy, and linkTo turns it into the URL from the request mappings, so nothing is hard-coded.
Then, if they want more
Then, only if order.isCancellable is true, I add linkTo of methodOn cancel id withRel cancel. With the HATEOAS starter, Boot renders HAL, so the response has the order's fields at the top level and a _links object with self and, for a cancellable order, cancel, each with an href. A shipped order only gets the self link.
The point is that the client shows the Cancel button when the cancel link is present and follows its href, so the cancellation rule lives only on the server.
In a real codebase I would move the link building into a RepresentationModelAssembler so every endpoint renders orders the same way, and for lists I would use CollectionModel, or PagedModel through PagedResourcesAssembler, which adds next and previous links and page metadata. I would also add @Relation on the DTO so the embedded list is called orders.
Behind a proxy, I would enable forwarded header support so the links use the public host.
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.
- Official documentation
- Spring Boot reference
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 "Using Spring HATEOAS, write a controller method that returns an order with a self link...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/4798d5de1b/