首页 > 解决方案 > 在camunda进程的进程变量中传递json

问题描述

我正在尝试将变量中的 json 有效负载作为值传递,以使用 engine-rest api 启动流程定义,如下所示:-

接口:

http://localhost:8080/engine-rest/process-definition/processService:1:9459dbe9-6b2c-11e8-b9e8-28d2447c697a/start

身体 :

{
  "variables": {
      "payload": {
        "value": {
            "mode": "email",
            "meta": [{
                "key": "topic",
                "value": "weather"
            }, {
                "key": "qos",
                "value": "2"
            }]
        },
        "type": "Json"
      }
  }
}

但它给出了 400 BAD REQUEST 并出现以下错误:- 必须为 SerializableValue 类型“Json”的值提供“null”或字符串值。

此外,我在我的 BPMN 流程中使用了一个表达式来获取如下所示的键值对,它也给我带来了错误:-

${S(payload).prop("mode").stringValue() == 'email'}

现在工作步骤:-当我尝试以字符串格式发送正文 json 有效负载时,它工作正常。

接口:

http://localhost:8080/engine-rest/process-definition/processService:1:9459dbe9-6b2c-11e8-b9e8-28d2447c697a/start

身体:

{
  "variables": {
      "payload": {
        "value": "{\"mode\": \"email\",\"meta\": [{\"key\": \"topic\",\"value\": \"weather\"},{\"key\": \"qos\",\"value\": \"2\"}]}",
        "type": "String"
      }
  }
}

我在这里使用相同的 java 代码来获取 json 有效负载-

public void notify(DelegateExecution delegateProcessExecution) throws Exception {
   Object notificationPayload =
       delegateProcessExecution.getVariable("payload");

    if (null != notificationPayload) {
        String notifyPayload = notificationPayload.toString();  
        JSONObject inputJson = new JSONObject(notifyPayload);
    }
    // ...
}

所以我希望这个有效载荷作为整个过程的 json,这样我就不需要像上面的工作示例那样将它转换为字符串。

标签: javascriptjavajsoncamundacamunda-modeler

解决方案


您应该只将类型更改为“json”,例如:

{
    "variables": {
        "broker": {
            "value": "{\"name\":\"Broker Name\"}",
            "type": "json"
        }
    }
}

推荐阅读