2. Enterprise Integration Patterns

groovy

Version

Runs a Groovy script against the current exchange to read or modify messages and steer the flow. The script runs in the request, response, and abort flows. Its return value decides what happens next:
  • an Outcome (CONTINUE, RETURN, ABORT) is used as-is;
  • a Response is set on the exchange and the flow returns;
  • a Request replaces the current request;
  • a Map is serialized to a JSON body;
  • a String or byte[] becomes the message body;
  • null or no value continues unchanged.
Bindings include exc/exchange, flow, message, header, body, request, response, params (request flow), statusCode (response flow), property, json (when the body is JSON), and spring for bean lookups. Provide the script inline as the element body, or load it from a file with location. See the examples under examples/scripting/groovy and the tutorial tutorials/advanced/70-Scripting-Groovy.yaml.
 groovy:
   [ location: <file-or-url> ]   # alternative to an inline script
   <groovy script>               # element body; omit when location is set
 

Example Configuration

api:
port: 2000
flow:
- groovy:
src: |
println "Running in the ${flow} flow"
header.add "X-Groovy", "true"
CONTINUE
api:
   port: 2000
   flow:
     - groovy:
         src: |
           println "Running in the ${flow} flow"
           header.add "X-Groovy", "true"
           CONTINUE

Syntax

groovy:
location: <string>
src: <string>
groovy:
  location: <string>
  src: <string>

Attributes

NameRequiredDefaultDescriptionExamples
locationfalseN/AA file or URL location where the content that should be set as body could be foundconf/body.txt
srcfalse---

Can be used in