Renders the body content of a message from a template. The template can produce plain text, Json or XML. Variables in the template are substituted with values from the body, header, query parameters, etc. If the extension of a referenced template file is
.xml it will use
XMLTemplateEngine otherwise
StreamingTemplateEngine . Have a look at the samples in
examples/template .
Sample Copy < api port = " 2000" > < request> < template contentType = " text/plain" > Hello ${params.name}!</ template> </ request> < return statusCode = " 200" /> </ api> < api name = " JSON" port = " 2000" method = " GET" > < request> < template contentType = " application/json" pretty = " yes" > { "answer": ${params.answer}, "foo": 7 } </ template> </ request> < return statusCode = " 200" /> </ api> < api port = " 2000" > < request> < template location = " template.xml" /> </ request> < return statusCode = " 200" /> </ api> < destinations xmlns: gsp= ' http://groovy.codehaus.org/2005/gsp' > < gsp: scriptlet> def answer = 42;</ gsp: scriptlet> < answer> < gsp: expression> answer</ gsp: expression> </ answer> < gsp: scriptlet> import groovy.xml.XmlSlurper def xml = new XmlSlurper().parseText(body) </ gsp: scriptlet> < gsp: scriptlet> xml.children().each { </ gsp: scriptlet> < destination> < gsp: expression> it</ gsp: expression> </ destination> < gsp: scriptlet> } </ gsp: scriptlet> </ destinations>
<api port="2000">
<request>
<template contentType="text/plain">Hello ${params.name}!</template>
</request>
<return statusCode="200"/>
</api><api name="JSON" port="2000" method="GET">
<request>
<template contentType="application/json" pretty="yes">
{
"answer": ${params.answer},
"foo": 7
}
</template>
</request>
<return statusCode="200"/>
</api><api port="2000">
<request>
<template location="template.xml"/>
</request>
<return statusCode="200"/>
</api><destinations xmlns:gsp='http://groovy.codehaus.org/2005/gsp'>
<gsp:scriptlet>def answer = 42;</gsp:scriptlet>
<answer><gsp:expression>answer</gsp:expression></answer>
<gsp:scriptlet>
import groovy.xml.XmlSlurper
def xml = new XmlSlurper().parseText(body)
</gsp:scriptlet>
<gsp:scriptlet>xml.children().each { </gsp:scriptlet>
<destination><gsp:expression>it</gsp:expression></destination>
<gsp:scriptlet> } </gsp:scriptlet>
</destinations>
Attributes Name Required Default Description Examples contentType false - - - location false - - - pretty false - - -
Can be used in