Skip to main content
Instead of a fixed response body, you can use a response template to build the response dynamically from the incoming request. MockServer supports three template engines:

Mustache

Simple syntax, sections for loops and conditionals. Works on all JVM versions.

Velocity

Apache Velocity — richer logic including loops, math, and JSON/XML parsing tools. Works on all JVM versions.

JavaScript

Uses the Nashorn engine. Full ES6 support on Java 9–14. Deprecated in Java 11, removed in Java 15+.
JavaScript templates require the Nashorn JavaScript engine, which was deprecated in Java 11 and removed in Java 15. Do not use JavaScript templates if you are running MockServer on Java 15 or later.

Request model variables

Every template has access to a request object with the following fields: Multi-value fields (headers, pathParameters, queryStringParameters) hold arrays. Access the first value with an index ([0] or .0).

Syntax by engine

Dynamic built-in variables

All engines also expose these variables. Each call produces a fresh value: Syntax follows the same engine conventions (e.g. {{ uuid }} in Mustache, $!uuid in Velocity, uuid in JavaScript).

Mustache templates

Mustache is the simplest option. Use {{ variable }} for substitution and {{# section }}...{{/ section }} for loops and conditionals.

Basic example

Variables

{{ request.method }} prints the method. If the variable is unresolvable, an empty string is returned.

Sections (loops, conditionals)

Sections iterate over arrays or act as if-statements. A section starts with {{# key }} and ends with {{/ key }}:
Use an inverted section {{^ -first }} to skip the first item (useful for comma-separated lists):

JsonPath in Mustache

Extract values from a JSON request body using {{# jsonPath }}:

XPath in Mustache

Extract values from an XML request body using {{# xPath }}:

Velocity templates

Velocity uses $variable notation and supports #if, #foreach, and tool access.

Basic example

Conditionals

Loops

Parsing JSON bodies

Use the built-in $json tool:

Velocity REST API example


JavaScript templates

JavaScript templates use Nashorn and must return a response object.

Basic example

Reading the body as a string

Use request.bodyAsString when you need the body as a plain string (useful for JSON parsing):

Conditional logic and delay

JavaScript templates support ES6 syntax on Java 9–14 (Nashorn). On Java 15 and later, Nashorn is not available and JavaScript templates will not work. Use Mustache or Velocity instead.