DEV Community

Alkademy
Alkademy

Posted on • Originally published at munonye.com

Axon Framework CQRS Tutorial — Step by Step (2026)

Canonical URL: Republished from munonye.com. Full microservices hub: munonye.com/microservices-tutorials/

What you'll build

  • Command side: CreateOrderCommandOrderAggregateOrderCreatedEvent
  • Event store: Axon Server
  • Query side: OrderProjection → H2 read model → REST GET

Prerequisites: Java 17+, Spring Boot 3.x, Axon Server


Step 1 — Dependencies

<dependency>
  <groupId>org.axonframework</groupId>
  <artifactId>axon-spring-boot-starter</artifactId>
  <version>4.9.3</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Step 2 — Configure Axon Server

axon.axonserver.servers=localhost:8124
spring.application.name=order-service
Enter fullscreen mode Exit fullscreen mode

Run ./axonserver — UI at http://localhost:8024.

Step 3 — Aggregate

@Aggregate
public class OrderAggregate {
  @AggregateIdentifier private String orderId;

  @CommandHandler
  public OrderAggregate(CreateOrderCommand cmd) {
    AggregateLifecycle.apply(new OrderCreatedEvent(cmd.getOrderId(), cmd.getProductId()));
  }

  @EventSourcingHandler
  public void on(OrderCreatedEvent event) {
    this.orderId = event.getOrderId();
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 4 — Query projection

@EventHandler
public void on(OrderCreatedEvent event) {
  repo.save(new OrderView(event.getOrderId(), event.getProductId(), "CREATED"));
}
Enter fullscreen mode Exit fullscreen mode

Step 5 — REST

  • POST /ordersCommandGateway.send(CreateOrderCommand)
  • GET /orders/{id} → read from OrderViewRepository

Full code walkthrough with curl tests: read on munonye.com →


About the author

Kindson MunonyeGitHub · LinkedIn · About

Top comments (0)