Flow ServerStarter
数据读写

脚本

type script

在设计器中打开 ↗

执行表达式并把返回值作为消息体

使用说明

要点:执行 Groovy 脚本,可读写 exchange 的 body / header / property。示例把消息体长度写入 Header,再由 setBody 输出;这样不依赖脚本返回值。

属性

字段类型必填默认值说明
lang
表达式语言
枚举 必填 groovy
可选值:constant、simple、groovy、header、exchangeProperty、method、jsonpath、xpath、spel
script
表达式内容
多行文本 必填 —

示例流程

可直接保存为 demo-script.json 并部署;非入口节点默认套了一个 REST 入口。

Flow JSON
{
  "id": "demo-script",
  "desc": "脚本 示例",
  "group": "示例/数据读写",
  "schemaVersion": 1,
  "nodes": [
    {
      "id": "n1",
      "type": "rest",
      "props": {
        "method": "POST",
        "path": "/demo/script",
        "desc": "示例入口"
      }
    },
    {
      "type": "script",
      "props": {
        "lang": "groovy",
        "script": "exchange.getMessage().setHeader('X-Len', exchange.getMessage().getBody(String.class).length())"
      },
      "id": "n1"
    },
    {
      "type": "setBody",
      "props": {
        "lang": "simple",
        "script": "length=${header.X-Len}"
      },
      "id": "n2"
    }
  ]
}
部署这段流程
curl -X POST 'http://localhost:8086/flow' \
  -H 'Content-Type: application/json' \
  -H 'X-Flow-Token: change-me' \
  --data-binary @demo-script.json

调用方式

如何触发:示例使用 REST 入口,部署后调用 POST http://localhost:8086/demo/script。

相关节点