英文:
I GET 404 ERROR IN AXON SAGA IMPLEMENTATION WHEN I SEND POST REQUEST.WHAT MIGHT BE THE REASON?
问题
I implemented SAGA using Axon and Spring Boot. It is a simple order and customer service. Everything looks fine; however, when I send a POST request via Postman, I got a 404 NOT FOUND error. I guess the problem is related to OrderManagementSaga.java. Please help me!
OrderManagementSaga.java
@Saga
public class OrderManagementSaga {
@Inject
private transient CommandGateway commandGateway;
@StartSaga
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderCreatedEvent orderCreatedEvent){
SagaLifecycle.associateWith("customerId", orderCreatedEvent.customerId);
commandGateway.send(new CreateInvoiceCommand(orderCreatedEvent.customerId, orderCreatedEvent.price, orderCreatedEvent.orderId));
}
@SagaEventHandler(associationProperty = "customerId")
public void handle(InvoiceCreatedEvent invoiceCreatedEvent){
SagaLifecycle.associateWith("orderId", invoiceCreatedEvent.orderId);
commandGateway.send(new UpdateOrderStatusCommand(invoiceCreatedEvent.orderId, invoiceCreatedEvent.price, invoiceCreatedEvent.customerId));
}
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderUpdatedEvent orderUpdatedEvent){
SagaLifecycle.end();
}
}
OrderCommandController.java
@RestController
public class OrderCommandController {
private OrderCommandService orderCommandService;
public OrderCommandController(OrderCommandService orderCommandService) {
this.orderCommandService = orderCommandService;
}
@PostMapping("/orders")
public CompletableFuture<String> createOrder(@RequestBody OrderCreateDTO orderCreateDTO){
return orderCommandService.createOrder(orderCreateDTO);
}
}
OrderAggregate.java
@Aggregate
public class OrderAggregate {
@AggregateIdentifier
private String orderId;
private BigDecimal price;
private OrderStatus orderStatus;
private String customerId;
public OrderAggregate() {
}
@CommandHandler
public OrderAggregate(CreateOrderCommand createOrderCommand) {
AggregateLifecycle.apply(new OrderCreatedEvent(createOrderCommand.orderId, createOrderCommand.price, createOrderCommand.customerId));
}
@EventSourcingHandler
protected void on(OrderCreatedEvent orderCreatedEvent) {
this.orderId = orderCreatedEvent.orderId;
this.customerId = orderCreatedEvent.customerId;
this.orderStatus = OrderStatus.CREATED;
}
@CommandHandler
protected void on(UpdateOrderStatusCommand updateOrderStatusCommand) {
AggregateLifecycle.apply(new OrderUpdatedEvent(updateOrderStatusCommand.orderId, updateOrderStatusCommand.customerId, updateOrderStatusCommand.price, OrderStatus.CREATED));
}
@EventSourcingHandler
protected void on(OrderUpdatedEvent orderUpdatedEvent) {
this.customerId = orderUpdatedEvent.customerId;
this.orderId = orderUpdatedEvent.orderId;
this.orderStatus = OrderStatus.APPROVED;
this.price = orderUpdatedEvent.price;
}
}
CustomerAggregate.java
@Aggregate
public class CustomerAggregate {
@AggregateIdentifier
private String customerId;
private BigDecimal budget;
private String orderId;
public CustomerAggregate(String customerId, BigDecimal budget, String orderId) {
super();
this.customerId = customerId;
this.budget = budget;
this.orderId = orderId;
}
@CommandHandler
public CustomerAggregate(CreateInvoiceCommand createInvoiceCommand){
AggregateLifecycle.apply(new InvoiceCreatedEvent(createInvoiceCommand.price, createInvoiceCommand.customerId, createInvoiceCommand.orderId));
}
@EventSourcingHandler
protected void on(InvoiceCreatedEvent invoiceCreatedEvent){
this.customerId = invoiceCreatedEvent.customerId;
this.budget = this.budget.subtract(invoiceCreatedEvent.price);
this.orderId = invoiceCreatedEvent.orderId;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public BigDecimal getBudget() {
return budget;
}
public void setBudget(BigDecimal budget) {
this.budget = budget;
}
}
I don't think the problem is related to command and event classes, so I only shared one of them as an example:
CreateOrderCommand.java
public class CreateOrderCommand {
@TargetAggregateIdentifier
public final String orderId;
public final String customerId;
public final BigDecimal price;
public CreateOrderCommand(String orderId, String customerId, BigDecimal price) {
super();
this.orderId = orderId;
this.customerId = customerId;
this.price = price;
}
}
英文:
i implemented SAGA using Axon and Spring Boot.It is simple order and customer service.Everything looks fine,however when i send post request via postman ,i got 404 NOT FOUND.I guess the problem is related to OrderManagementSaga.java Please help me!
OrderManagementSaga.java
@Saga
public class OrderManagementSaga {
@Inject
private transient CommandGateway commandGateway;
@StartSaga
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderCreatedEvent orderCreatedEvent){
SagaLifecycle.associateWith("customerId", orderCreatedEvent.customerId);
commandGateway.send(new CreateInvoiceCommand(orderCreatedEvent.customerId,orderCreatedEvent.price,orderCreatedEvent.orderId));
}
@SagaEventHandler(associationProperty = "customerId")
public void handle(InvoiceCreatedEvent invoiceCreatedEvent){
SagaLifecycle.associateWith("orderId",invoiceCreatedEvent.orderId);
commandGateway.send(new UpdateOrderStatusCommand(invoiceCreatedEvent.orderId, invoiceCreatedEvent.price, invoiceCreatedEvent.customerId));
}
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderUpdatedEvent orderUpdatedEvent){
SagaLifecycle.end();
}
}
OrderCommandController.java
@RestController
public class OrderCommandController {
private OrderCommandService orderCommandService;
public OrderCommandController(OrderCommandService orderCommandService) {
this.orderCommandService = orderCommandService;
}
@PostMapping("/orders")
public CompletableFuture<String> createOrder(@RequestBody OrderCreateDTO orderCreateDTO){
return orderCommandService.createOrder(orderCreateDTO);
}
}
OrderAggregate.java
@Aggregate
public class OrderAggregate {
@AggregateIdentifier
private String orderId;
private BigDecimal price;
private OrderStatus orderStatus;
private String customerId;
public OrderAggregate() {
}
@CommandHandler
public OrderAggregate(CreateOrderCommand createOrderCommand) {
AggregateLifecycle.apply(new OrderCreatedEvent(createOrderCommand.orderId,createOrderCommand.price, createOrderCommand.customerId));
}
@EventSourcingHandler
protected void on(OrderCreatedEvent orderCreatedEvent) {
this.orderId = orderCreatedEvent.orderId;
this.customerId = orderCreatedEvent.customerId;
this.orderStatus = OrderStatus.CREATED;
}
@CommandHandler
protected void on(UpdateOrderStatusCommand updateOrderStatusCommand) {
AggregateLifecycle
.apply(new OrderUpdatedEvent(updateOrderStatusCommand.orderId,updateOrderStatusCommand.customerıd,updateOrderStatusCommand.price,OrderStatus.CREATED));
}
@EventSourcingHandler
protected void on(OrderUpdatedEvent orderUpdatedEvent) {
this.customerId = orderUpdatedEvent.customerId;
this.orderId = orderUpdatedEvent.orderId;
this.orderStatus = OrderStatus.APPROVED;
this.price = orderUpdatedEvent.price;
}
CustomerAggregate.java
@Aggregate
public class CustomerAggregate {
@AggregateIdentifier
private String customerId;
private BigDecimal budget;
private String orderId;
public CustomerAggregate(String customerId, BigDecimal budget, String orderId) {
super();
this.customerId = customerId;
this.budget = budget;
this.orderId = orderId;
}
@CommandHandler
public CustomerAggregate(CreateInvoiceCommand createInvoiceCommand){
AggregateLifecycle.apply(new InvoiceCreatedEvent(createInvoiceCommand.price, createInvoiceCommand.customerId, createInvoiceCommand.orderId));
}
@EventSourcingHandler
protected void on(InvoiceCreatedEvent invoiceCreatedEvent){
this.customerId = invoiceCreatedEvent.customerId;
this.budget = this.budget.subtract(invoiceCreatedEvent.price);
this.orderId = invoiceCreatedEvent.orderId;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public BigDecimal getBudget() {
return budget;
}
public void setBudget(BigDecimal budget) {
this.budget = budget;
}
}
I don't think the problem is related with command and event classes ,so i only share one of them as example;
CreateOrderCommand.java
public class CreateOrderCommand {
@TargetAggregateIdentifier
public final String orderId;
public final String customerId;
public final BigDecimal price;
public CreateOrderCommand(String orderId, String customerId, BigDecimal price) {
super();
this.orderId = orderId;
this.customerId = customerId;
this.price = price;
}
}
答案1
得分: 1
404表示您首先无法访问REST控制器。这能够进行调试和检查吗?如果这正常运行,那么可以更详细地检查控制器的实现:注入到REST控制器中的orderCommandService
是什么?希望它正在使用Axon命令网关将命令发送到OrderAggregate
。OrderAggregate
将发布一个类型为OrderCreatedEvent
的事件,这应该会启动Saga。
英文:
404 indicates that you can not reach the REST controller in the first place. Can this be debugged and checked? If this is working as expected, then the implementation of this controller could be examined in more detail: what is orderCommandService
that is injected in the REST controller. Hopefully, it is using the Axon command gateway to send the command to OrderAggregate
. The OrderAggregate
will publish an event of type OrderCreatedEvent
which should start the Saga.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论