Rendering and JSON

Learn about different ways to render a response.

There are many ways to render a response.

Groovy text

If you prefer to use a plain old text document with embedded groovy (much like a GString), you can use the TextTemplateModule:

import ratpack.groovy.template.TextTemplateModule;
import static ratpack.groovy.Groovy.groovyTemplate;
import static ratpack.groovy.Groovy.ratpack;
ratpack {
    bindings {
        module TextTemplateModule
    }
    handlers {
        get(":key") {
            def key = pathTokens.key
            render groovyTemplate("index.html", title: "$key")
        }
    }
}

Then create a file named index.html in the src/main/resources/templates directory with the following content:

<html><h2>${model.title}</h2></html>

It supplies a model Map to your template, which contains all of the parameters you supply.

Handlebars and thymeleaf

Ratpack also supports handlebars and thymeleaf templates. Since these are static resources, we should put these templates in the src/main/resource directory.

We will first need to include the appropriate ratpack project in your Gradle build file:

runtime 'io.ratpack:ratpack-handlebars:1.1.1'
runtime 'io.ratpack:ratpack-thymeleaf:1.1.1'

To use Handlebars, include the HandlebarsModule, and render handlebar templates as follows:

import ratpack.handlebars.HandlebarsModule
import static ratpack.handlebars.Template.handlebarsTemplate
import static ratpack.groovy.Groovy.ratpack
ratpack {
    bindings {
        module HandlebarsModule
    }
    handlers {
        get("foo") {
            render handlebarsTemplate('myTemplate.html', title: 'Handlebars')
        }
    }
}

Create a file named myTemplate.html.hbs in the src/main/resources/handlebars directory with handlebar content. For example:

<span>{{title}}</span>

Thymeleaf works in a similar way:

import ratpack.thymeleaf.ThymeleafModule
import static ratpack.thymeleaf.Template.thymeleafTemplate
import static ratpack.groovy.Groovy.ratpack
ratpack {
    bindings {
        module ThymeleafModule
    }
    handlers {
        get("foo") {
            render thymeleafTemplate('myTemplate', title: 'Thymeleaf')
        }
    }
}

The requested file should be named myTemplate.html and be located in the src/main/resources/thymeleaf directory of our project. Example content:

<span th:text=\"${title}\"/>

JSON

Integration with the Jackson JSON marshalling library provides the ability to work with JSON as part of ratpack-core.

The ratpack.jackson.Jackson class provides most of the Jackson related functionality. For example, to render json you can use Jackson.json:

import static ratpack.jackson.Jackson.json
ratpack {
    bindings {}
    handlers {
        get("user") {
            render json([user: 1])
        }
    }
}

The Jackson integration also includes a Parser for converting JSON request bodies into objects. The Jackson.jsonNode() and Jackson.fromJson(Class) methods can be used to create parcelable objects to be used with the parse() method.

For example, the following handler would parse a JSON string representing a Person object and render the Person’s name:

post("personNames") {
   render( parse(fromJson(Person.class)).map {it.name} )
}

Context.parse returns a Promise.

Get hands-on with 1400+ tech skills courses.