Go to new doc!

+49 228 5552576-0


info@predic8.com

template

Description

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 .

Can be used in

spring:beans, api, bean, if, interceptor, internalProxy, proxy, registration, request, response, serviceProxy, soapProxy, stompProxy, swaggerProxy, transport and wsStompReassembler

Sample

Text Template

Call it with a query parameter name:

http://localhost:2000?name=Joe

<api port="2000">
	<request>
		<template contentType="text/plain">Hello ${params.name}!</template>
	</request>
	<return statusCode="200"/>
</api>
JSON Template

Call it with the answer.

http://localhost:2000?answer=42

<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>
XML Template

Send a POST with an XML body content to the api.

<api port="2000">
	<request>
		<template location="template.xml"/>
	</request>
	<return statusCode="200"/>
</api>

If the filename of the template ends with .xml you can use the elements from the gsp namespace. See GroovyXMLTemplate Engine

<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 Example
contentType false - content type for body application/json
location false - path of xml template file. template.xml
pretty false no Format JSON documents. yes

Variables

The following variables can be accessed within a template.

Name Class Description
exc com.predic8.membrane.core.exchange.Exchange The Exchange class provides, among others, access to the Request, Response and their corresponding Headers.
message com.predic8.membrane.core.http.Message Message is the superclass of a request or a response. Depending on the flow the message contains data from the request or the response.
header com.predic8.membrane.core.http.Header The header object allows access to the HTTP header fields. You can remove, add and change fields.
body com.predic8.membrane.core.http.Body The body of the message.
json java.util.Map If the message contains a body of the type application/json this varible will contain the JSON as a Map. Use it only to read the JSON document.
flow com.predic8.membrane.core.interceptor.Flow REQUEST or RESPONSE
properties java.util.Map The properties of the exchange object.
spring org.springframework.context.ApplicationContext The Spring application context hosting the router instance (or null, if the router is not hosted by Spring).