在Kogito的Serverless Workflow foreach状态调用REST接口遇问题求助
Kogito + Serverless Workflow foreach状态问题排查与修复
问题描述
1. 核心业务异常
${ .item }变量始终为null,导致REST调用参数错误- 将
${ .item }替换为固定值后,outputCollection: .greetings未收集两次迭代调用的结果,返回空数组
2. 工作流调用命令
curl -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d '{"workflowdata" : {"name": "Helber", "language": ["Spanish","English"]}}' http://localhost:8080/greeting
3. 当前工作流定义
{ "id": "greeting", "version": "1.0", "specVersion": "0.8", "name": "Greeting workflow", "description": "JSON based greeting workflow", "start": "Loop", "functions": [ { "name": "getGreetingFunction", "operation": "openapi.yml#getGreeting" } ], "states": [ { "name": "Loop", "type": "foreach", "inputCollection": ".languages", "iterationParam": "item", "outputCollection": ".greetings", "actions": [ { "name": "getGreeting", "functionRef": { "refName": "getGreetingFunction", "arguments": { "name": "${ .name }", "language": "${ .item }" } } } ], "end": true } ] }
4. 错误日志输出
2022-09-02 17:15:55,227 WARN [org.kog.wor.res.RestWorkItemHandler] (executor-thread-0) Cannot find definition for variable Loop_5 2022-09-02 17:15:55,296 ERROR [org.jbp.wor.ins.imp.NodeInstanceImpl] (executor-thread-0) Could not find variable Loop_5 2022-09-02 17:15:55,296 ERROR [org.jbp.wor.ins.imp.NodeInstanceImpl] (executor-thread-0) Using process-level scope 2022-09-02 17:15:55,298 INFO [org.kie.kog.exp.jq.JqExpressionHandler] (executor-thread-0) Using default scope
问题修复方案
1. 修正inputCollection的字段路径错误
调用请求中传入的数组字段是language,但工作流定义里写的是.languages(多了一个s),这是导致item变量为null的直接原因:
- 原定义:
inputCollection: ".languages" - 修正为:
inputCollection: ".language"
确保遍历的目标数组存在,item变量才会被正确赋值为数组的每个元素。
2. 配置动作输出映射,填充outputCollection
Kogito的foreach状态不会自动收集函数返回值,需要通过actionDataFilter明确指定迭代动作的输出内容,才能将结果写入outputCollection:
"actions": [ { "name": "getGreeting", "functionRef": { "refName": "getGreetingFunction", "arguments": { "name": "${ .name }", "language": "${ .item }" } }, "actionDataFilter": { "results": "${ .greeting }" // 对应getGreetingFunction返回的greeting字段 } } ]
3. 日志中Loop_5变量的处理
该错误是Kogito生成foreach内部变量时的作用域问题,修复前两个核心问题后,该日志错误通常会自动消失。如果仍存在,建议:
- 升级Kogito到与specVersion 0.8兼容的稳定版本(推荐1.20.x及以上)
- 确保项目依赖的Quarkus版本与Kogito版本匹配
修复后的完整工作流
{ "id": "greeting", "version": "1.0", "specVersion": "0.8", "name": "Greeting workflow", "description": "JSON based greeting workflow", "start": "Loop", "functions": [ { "name": "getGreetingFunction", "operation": "openapi.yml#getGreeting" } ], "states": [ { "name": "Loop", "type": "foreach", "inputCollection": ".language", "iterationParam": "item", "outputCollection": ".greetings", "actions": [ { "name": "getGreeting", "functionRef": { "refName": "getGreetingFunction", "arguments": { "name": "${ .name }", "language": "${ .item }" } }, "actionDataFilter": { "results": "${ .greeting }" } } ], "end": true } ] }
验证结果
重新部署后执行原curl命令,会得到包含正确greetings数组的输出,示例:
{ "id": "xxx-xxx-xxx", "workflowdata": { "name": "Helber", "language": ["Spanish","English"], "greetings": [ "Greetings from Serverless Workflow, Helber! (Spanish)", "Greetings from Serverless Workflow, Helber! (English)" ] } }
内容的提问来源于stack exchange,提问作者Cyril C.
相关产品推荐
相关产品推荐

