Skip to content

rrezartprebreza/spring-boot-skills / skills/spring-boot-3/hateoas

hateoas

Verified

Use when adding hypermedia links to REST responses, building self-describing APIs, or implementing Spring HATEOAS. Use when you see EntityModel, CollectionModel, or RepresentationModel in the project.

examples/bad-hateoas-controller.java

View on GitHub
// BAD — hardcoded URLs, all links regardless of state, no assembler, missing self link

@RestController
@RequestMapping("/api/v1/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/{id}")
    public OrderResponse getById(@PathVariable UUID id) {
        Order order = orderService.findById(id);
        OrderResponse response = OrderResponse.from(order);
        // Returns plain DTO — no HATEOAS links at all!
        return response;
    }

    @GetMapping("/{id}/with-links")
    public Map<String, Object> getWithLinks(@PathVariable UUID id) {
        Order order = orderService.findById(id);
        return Map.of(
            "data", OrderResponse.from(order),
            "_links", Map.of(
                // BAD: Hardcoded URLs instead of linkTo(methodOn(...))
                "self", "http://localhost:8080/api/v1/orders/" + id,
                "orders", "http://localhost:8080/api/v1/orders",
                // BAD: All action links regardless of order state
                "cancel", "http://localhost:8080/api/v1/orders/" + id + "/cancel",
                "ship", "http://localhost:8080/api/v1/orders/" + id + "/ship",
                "confirm", "http://localhost:8080/api/v1/orders/" + id + "/confirm"
            )
        );
    }

    @GetMapping
    public List<OrderResponse> list(Pageable pageable) {
        // BAD: Returns plain list — no CollectionModel, no pagination links
        return orderService.findAll(pageable).getContent().stream()
            .map(OrderResponse::from)
            .toList();
    }

    // BAD: Link-building logic scattered in controller instead of assembler
}
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall