Grails Goodness: Include Domain Version Property in JSON and XML Output
We can include the version
property of domain classes in rendered JSON or XML output. By default the version
property is not included in generated XML and JSON. To enable inclusion of the version
property we can set the configuration property grails.converters.domain.include.version
with the value true
. With this property for both XML and JSON the version
property is included in the output. To only enable this for XML we use the property grails.converters.xml.domain.include.version
and for JSON we use the property grails.converters.json.domain.include.version
.
The following snippet is from grails-app/conf/Config.groovy
where the properties are defined:
...
// Include version for JSON generated output.
//grails.converters.json.domain.include.version = true
// Include version for XML generated output.
//grails.converters.xml.domain.include.version = true
// Include version for both XML and JSON output.
grails.converters.domain.include.version = true
...
The following samples show a representation of a book resources, which is a domain class and we see the version
property as attribute of the book XML tag.
0451169514
1104
It
In JSON the version is included as property.
{
"class": "com.mrhaki.grails.sample.Book",
"id": 1,
"version": 0,
"isbn": "0451169514",
"numberOfPages": 1104,
"title": "It"
}
Code written with Grails 2.3.2.