Camunda流程引擎中ScriptsTask与http-connector对流转变量处理操作

一、http-connector中对已有变量的更新方式

例如:当http-connector 返回信息如下,如何获取json格式并更新已经以java object类型(Map)存储的流程变量

{
  "data": {
    "role": "admin",
    "user": {
      "avatar": "12312",
      "createTime": "2018-12-31 22:31:33",
      "email": "[email protected]",
      "id": "1566443635598",
      "loginName": "zhangsan",
      "mobile": "13122099921",
      "name": "张三"
    }
  },
  "message": "操作成功",
  "stat": "1"
}

Camunda流程引擎中ScriptsTask与http-connector对流转变量处理操作_第1张图片

方式1:


/**获取connector「对象类型节点」变量**/
var u = S(connector.getVariable("response")).jsonPath("data.user").element();
/**JSON映射为java map类型 提交流程变量**/
org.camunda.spin.Spin.JSON(u).mapTo(java.util.Map.class);

方式2:

var u = S(connector.getVariable("response")).jsonPath("data.user").element();
new com.fasterxml.jackson.databind.ObjectMapper().readValue(u, java.util.Map.class);

二、ScriptTask环节对已有变量修改方式

例如:变量当前结构:

{

 "user": {
  "avatar": "12312",
  "createTime": "2019-12-31 12:31:33",
  "email": "[email protected]",
  "id": "1566443635598",
  "loginName": "zhangsan",
  "mobile": "13122099921",
  "name": "张三"
 }
}

Camunda流程引擎中ScriptsTask与http-connector对流转变量处理操作_第2张图片

1.直接修改user变量中某个属性:

var v = execution.getVariable('user');
v.email = '[email protected]';

2.user变量中新增个数组对象属性:

var v = execution.getVariable('user');
var role = new java.util.ArrayList();
role.add("admin");
role.add("manage");
v.role = role;

3.其他

// user是按照java object的方式传入的 
var v = S(execution.getVariable("user"));
var obj = JSON.parse(org.camunda.spin.Spin.JSON(v));
obj.bbb = JSON.parse('{"arr":[1,2,3]}');
execution.setVariable('user2', S(JSON.stringify(obj)).mapTo(java.util.Map.class));

你可能感兴趣的:(Camunda,流转变量,http-connector,ScriptTask)