You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将CreateOrderController映射到Camunda的CreateOrder任务并传参

将Spring Boot RestController与Camunda CreateOrder任务集成并传递参数

1. 引入Camunda Spring Boot依赖

确保项目pom.xml中包含Camunda Spring Boot Starter(未添加的话补充):

<dependency>
    <groupId>org.camunda.bpm.springboot</groupId>
    <artifactId>camunda-bpm-spring-boot-starter</artifactId>
    <version>7.21.0</version> <!-- 使用与项目匹配的版本 -->
</dependency>

2. 修改RestController,集成Camunda RuntimeService

调整CreateOrderController,注入Camunda的RuntimeService来启动流程实例,并将OrderInfo作为流程变量传入Camunda:

@RestController
public class CreateOrderController {

    private final RuntimeService runtimeService;

    // 构造注入RuntimeService(Spring自动装配)
    public CreateOrderController(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }

    @PostMapping("/rest/create/order")
    public String createOrder(@RequestBody OrderInfo orderInfo) {
        System.out.println("Order created with Order id: " + orderInfo.getOrderId());
        
        // 启动指定key的流程实例,传入OrderInfo作为流程变量
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(
            "order-processing", // 替换为你的BPMN流程定义key
            Map.of("orderInfo", orderInfo)
        );
        
        return "Order created with id: " + orderInfo.getOrderId() + ", Process instance ID: " + processInstance.getId();
    }
}

3. 确保OrderInfo类可序列化

Camunda存储流程变量要求对象支持序列化,让OrderInfo实现Serializable接口:

import java.io.Serializable;

public class OrderInfo implements Serializable {
    private String orderId;
    // 其他业务字段、getter和setter方法
    
    public String getOrderId() {
        return orderId;
    }
    
    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
}

4. 配置Camunda BPMN流程

在BPMN流程文件中:

  • 定义流程key为order-processing(与Controller中调用的key一致)
  • 第一个任务设置为CreateOrder(如果是自动执行的服务任务,可关联业务逻辑;如果是用户任务,需后续手动完成)
  • 连接CreateOrder任务到下一个Place Order任务

为Place Order任务绑定执行逻辑

创建JavaDelegate类处理Place Order的业务逻辑,并从流程变量中获取OrderInfo:

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Component;

@Component
public class PlaceOrderDelegate implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        // 从流程变量中取出OrderInfo对象
        OrderInfo orderInfo = (OrderInfo) execution.getVariable("orderInfo");
        
        // 执行下单业务逻辑
        System.out.println("Placing order for ID: " + orderInfo.getOrderId());
        // 这里可添加实际下单操作,比如调用外部服务、写入数据库等
    }
}

在BPMN编辑器中,将Place Order任务的Type设置为Java Class,指定类路径com.yourpackage.PlaceOrderDelegate;或使用Delegate Expression,填写${placeOrderDelegate}(Spring会自动注入该Bean)。

5. 验证流程执行

当GUI调用/rest/create/order接口时:

  1. Controller接收OrderInfo并打印日志
  2. Camunda启动流程实例,将OrderInfo存入流程变量
  3. 流程自动执行CreateOrder任务(服务任务场景),随后流转到Place Order任务
  4. PlaceOrderDelegate获取OrderInfo并执行业务逻辑

内容的提问来源于stack exchange,提问作者Hello

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 20:15:41