The Link class in Spring HATEOAS (org.springframework.hateoas.Link) represents one hypermedia link: a target URI (href) plus a relation type (rel) that tells the client what the link means. Adding links such as self, next or orders to API responses lets clients follow URLs the server provides instead of hardcoding them, which is the core of HATEOAS (Hypermedia as the Engine of Application State).
Link is an immutable value object. You attach it to a RepresentationModel (or its subclasses EntityModel and CollectionModel), and Spring HATEOAS renders it in a media type such as HAL.
| Property | Meaning | Example |
|---|---|---|
href | Target URI, possibly a URI template | /products/42 |
rel | Relation type | self, next, orders |
title, type, hreflang | Optional hints | Product detail |
deprecation, profile, name | Optional metadata |
Because the class is immutable, every withXxx method returns a new instance. Relations should come from the IANA registry when one fits (IanaLinkRelations.SELF, NEXT, PREV, COLLECTION, EDIT), and use your own name only for domain-specific actions.
Hardcoded strings break when a path changes, so build links from controller methods wherever possible. search above is a templated link: search.isTemplated() is true, and search.expand(0, 20) produces /products?page=0&size=20.
WebMvcLinkBuilder.linkTo(methodOn(...)) derives the URI from your @RequestMapping annotations, so the link follows the controller:
With spring-boot-starter-hateoas on the classpath and an Accept: application/hal+json header (the default for hypermedia responses), the single product renders as:
The original example of an update link pointing to another URL is misleading: HTTP methods are not part of a Link. A client PUTs to the same self URI. If you want to advertise available actions with their methods and input, use Spring HATEOAS affordances (.andAffordance(afford(methodOn(...).update(...)))) together with a media type such as HAL-FORMS.
self: the canonical URI of the current resource.next, prev, first, last: pagination.collection: the collection the item belongs to.edit: a URI for modifying the resource.reviews or cancel: domain links, ideally documented in your API docs. Spring HATEOAS also supports curies to namespace them in HAL.Assert on the model rather than parsing JSON:
getRequiredLink throws if the relation is missing, and getLink returns an Optional.
It is designed for hypermedia APIs, but you can use it for any response that benefits from navigation links, such as paginated lists.
href is where the resource is, rel is why it matters. Clients should code against the rel and treat the href as opaque.
linkTo builds absolute URIs from the current request (respecting forwarded headers). Link.of("/path", rel) stays as you wrote it.
Yes. Use WebFluxLinkBuilder for reactive controllers.
Link is the small immutable building block of Spring HATEOAS: an href plus a rel. Create links with linkTo(methodOn(...)), prefer IANA relations, attach them to EntityModel or CollectionModel, and use affordances when you need to describe actions rather than navigation.