当我发送 POST 请求时,我在 AXON SAGA 实施中收到 404 错误。可能是啥原因? [关闭]

Posted

技术标签:

【中文标题】当我发送 POST 请求时,我在 AXON SAGA 实施中收到 404 错误。可能是啥原因? [关闭]【英文标题】:I GET 404 ERROR IN AXON SAGA IMPLEMENTATION WHEN I SEND POST REQUEST.WHAT MIGHT BE THE REASON? [closed]当我发送 POST 请求时,我在 AXON SAGA 实施中收到 404 错误。可能是什么原因? [关闭] 【发布时间】:2020-11-29 23:32:06 【问题描述】:

我使用 Axon 和 Spring Boot 实现了 SAGA。这是简单的订单和客户服务。一切看起来都很好,但是当我通过邮递员发送帖子请求时,我得到 404 NOT FOUND。我猜这个问题与 OrderManagementSaga.java 有关请帮帮我!

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;
    
    
    

我认为问题与命令和事件类无关,所以我只分享其中一个作为示例; 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】:

404 表示您无法访问 REST 控制器。可以调试和检查吗?如果这按预期工作,则可以更详细地检查此控制器的实现:注入 REST 控制器的orderCommandService 是什么。希望它使用 Axon 命令网关将命令发送到OrderAggregateOrderAggregate 将发布一个 OrderCreatedEvent 类型的事件,该事件应该启动 Saga。

【讨论】:

请注意 orderCommandService 必须声明为 Spring Bean 才能注入 REST 控制器

以上是关于当我发送 POST 请求时,我在 AXON SAGA 实施中收到 404 错误。可能是啥原因? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

当我在 python 中发送发布请求时......我该怎么做

React js 和 axios POST 请求发送空数组

无法使用 axios 发送 http POST 请求

Axios Post 请求发送未定义

当我发送 POST 请求时被禁止 403

发送 AJAX 请求时不显示引导模式