在camunda进程中的进程变量中传递json

问题描述 投票:0回答:3

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

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 并带有以下错误:- 必须为 SerializedValue 类型“Json”的值提供“null”或字符串值。

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

${S(payload).prop("mode").stringValue() == '电子邮件'}

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

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": "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,这样我就不需要像上面的工作示例一样将其转换为字符串。

javascript java json camunda camunda-modeler
3个回答
4
投票

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

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

1
投票

这是 REST 引擎 API 中的设计,它们也支持其他数据格式,因此它必须是转义的 JSON 字符串,请参阅https://app.camunda.com/jira/browse/CAM-9617

解决方案是将转义的 JSON 字符串作为值传递,正如您上面指出的那样。如果引擎的类路径上有一个与给定值匹配的 Jackson Java Bean,也可以使用

"type": "Object"
。您在
valueInfo
对象中提供 bean 类型名称:

https://docs.camunda.org/manual/7.10/reference/rest/process-definition/post-start-process-instance/#request-body

例如:

{
  "variables": {
      "payload": {
        "value": "{\"mode\": \"email\",\"meta\": [{\"key\": \"topic\",\"value\": \"weather\"},{\"key\": \"qos\",\"value\": \"2\"}]}",
        "type": "String",
        "valueInfo": {
            "objectTypeName": "my.own.BeanWithModeAndMetaProps",
            "serializationDataFormat": "application/json"
            }
      }
  }
}

0
投票

这对我有用。

{ "variables": { "payload": { "value": "{\\"mode\\": \\"email\\",\\"meta\\": [{\\"key\\": \\"topic\\",\\"value\\": \\"weather\\"},{\\"key\\": \\"qos\\",\\"value\\": \\"2\\"}]}", "type": "String", "valueInfo": { "objectTypeName": "my.own.BeanWithModeAndMetaProps", "serializationDataFormat": "application/json" } } } }

© www.soinside.com 2019 - 2024. All rights reserved.