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

如何在Camunda中将ValidateRequest配置为独立Service Task并实现请求验证响应?

问题描述

现有需求如下:

  1. 调用http://localhost:8080/order/{id}接口触发工作流启动;
  2. 通过独立Java类ValidateRequest完成请求验证;
  3. 验证完成后返回“Hello World”或对应的错误信息。

目前已有可正常运行的工作流,希望将ValidateRequest设置为独立的Service Task,询问具体实现方法。

现有代码

控制器类 MyTestRestController

@RestController
public class MyTestRestController {

@Autowired
private RuntimeService runtimeService;

@Autowired
private TaskService taskService;

@Autowired
private ValidateRequest validateRequest;

@GetMapping("/order/{id}")
public String test(@PathVariable int id) throws Exception {

  Map<String, Object> controlParameters = new HashMap<>();

  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("hello-world-process",controlParameters);

  if (id == 1) {

    controlParameters.put("errorFlag", true);
    completeTask(processInstance, controlParameters);
    throw new IllegalArgumentException(" Illegal Argument ");
  }

  controlParameters.put("errorFlag", false);
  completeTask(processInstance, controlParameters);

  return "Hello World";
}
}

验证服务类 ValidateRequest

@Service
public class ValidateRequest implements JavaDelegate{


@Override
public void execute(DelegateExecution exec) throws Exception {
    // TODO Auto-generated method stub
     int id =1;
    
    System.out.println(" Starting ValidateRequest");
    
   Thread.sleep(10000);

   if(id==1)
   {
       throw new IllegalArgumentException(" Illegal Argument 2 ");
   }
   
    
}
}

实现步骤

1. 修改工作流BPMN模型

在目标工作流中添加Service Task,并完成以下配置:

  • 设置任务实现方式为Java class,选择ValidateRequest类;
  • 添加输入映射:将接口传入的id作为流程变量,映射到DelegateExecution中,确保验证类能获取到该参数;
  • 添加边界错误事件:为Service Task绑定边界错误事件,捕获IllegalArgumentException,并指向错误结束节点(比如命名为errorEndEvent),标记流程因验证失败终止。

2. 调整控制器逻辑

移除控制器中手动验证和completeTask的硬编码逻辑,改为启动工作流时传入id参数,并根据流程执行结果返回对应信息:

@RestController
public class MyTestRestController {

@Autowired
private RuntimeService runtimeService;

@Autowired
private HistoryService historyService;

@GetMapping("/order/{id}")
public String test(@PathVariable int id) throws Exception {
  // 传入id作为流程变量
  Map<String, Object> variables = new HashMap<>();
  variables.put("id", id);

  // 启动工作流
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("hello-world-process", variables);

  // 同步等待流程执行完成(生产环境建议用异步回调或消息通知)
  while (historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).finished().count() == 0) {
      Thread.sleep(100);
  }

  // 根据流程结束节点判断结果
  HistoricProcessInstance historicInstance = historyService.createHistoricProcessInstanceQuery()
          .processInstanceId(processInstance.getId())
          .singleResult();

  if ("errorEndEvent".equals(historicInstance.getEndActivityId())) {
      return "验证失败:参数非法";
  } else {
      return "Hello World";
  }
}
}

3. 完善验证类逻辑

修改ValidateRequest,从流程变量中获取真实的id参数并执行验证:

@Service
public class ValidateRequest implements JavaDelegate{

@Override
public void execute(DelegateExecution exec) throws Exception {
    // 从流程变量中获取接口传入的id
    Integer id = (Integer) exec.getVariable("id");
    
    System.out.println("Starting ValidateRequest with id: " + id);
    
    // 模拟验证耗时
    Thread.sleep(10000);

    // 执行真实验证逻辑
    if(id != null && id == 1) {
        throw new IllegalArgumentException("Illegal Argument");
    }
}
}

4. 配置错误处理流程

在BPMN模型中,确保边界错误事件正确关联IllegalArgumentException,并将错误流指向错误结束节点,这样流程在验证失败时会自动终止到该节点,方便控制器通过历史记录判断结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 21:45:32