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.

templates/OrderModelAssembler.java

View on GitHub
package com.example.order.controller;

import com.example.order.dto.response.OrderResponse;
import com.example.order.entity.Order;
import com.example.order.entity.OrderStatus;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.RepresentationModelAssembler;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@Component
public class OrderModelAssembler
        implements RepresentationModelAssembler<Order, EntityModel<OrderResponse>> {

    @Override
    @NonNull
    public EntityModel<OrderResponse> toModel(@NonNull Order order) {
        EntityModel<OrderResponse> model = EntityModel.of(
            OrderResponse.from(order),
            // Self link — always present
            linkTo(methodOn(OrderController.class).getById(order.getId())).withSelfRel(),
            // Collection link
            linkTo(methodOn(OrderController.class).list(null, null)).withRel("orders"));

        // Conditional action links based on current state
        OrderStatus status = order.getStatus();

        if (status == OrderStatus.PENDING) {
            model.add(linkTo(methodOn(OrderController.class)
                .confirm(order.getId())).withRel("confirm"));
            model.add(linkTo(methodOn(OrderController.class)
                .cancel(order.getId())).withRel("cancel"));
        }

        if (status == OrderStatus.CONFIRMED) {
            model.add(linkTo(methodOn(OrderController.class)
                .ship(order.getId())).withRel("ship"));
        }

        if (status == OrderStatus.SHIPPED) {
            model.add(linkTo(methodOn(OrderController.class)
                .deliver(order.getId())).withRel("deliver"));
        }

        return model;
    }
}
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