(Quick Reference)

1.1.4 JSON Views 1.1

Version: 3.2.8

1.1.4 JSON Views 1.1

Version 1.1 of the JSON Views plugin is included with Grails 3.2’s "rest-api" profile and includes a number of new features. Below are some of the highlights:

Template Inheritance

It is now possible for a child JSON template to specify a parent template, thus allowing better template composition. For example given the following parent:

grails-app/views/_parent.gson
model {
    Object object
}
json {
    hal.links(object)
    version "1.0"
}

A child template can be specified as follows:

inherits template:"parent"
model {
    Person person
}
json {
    name person.name
}

Global and Default Templates

Global templates can now be created for any GORM custom types. This allows adding support for external libraries such as JodaTime or custom types provided by datastores such as MongoDB (example GeoJSON).

A global template is simply another JSON template that is named after the class name. See for example the GeoJSON templates.

In addition it is now possible to provide a fallback template named /object/_object.gson that is used if no other template is found.

Better HAL Support

The HAL support has been expanded and now includes greater control over _embedded and _links, for example:

model {
    Book book
}
json {
    hal.links(self: book )
    hal.embedded(authors: book.authors)
    hal.inline(book) {
        pages 300
    }
}

The HAL support has also been improved with support for HAL pagination.