Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Grails Goodness: Include Domain Version Property in JSON and XML Output

DZone's Guide to

Grails Goodness: Include Domain Version Property in JSON and XML Output

· Java Zone ·
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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 propertygrails.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.

<?xml version="1.0" encoding="UTF-8"?>
<book id="1" version="0">
  <isbn>
    0451169514
  </isbn>
  <numberOfPages>
    1104
  </numberOfPages>
  <title>
    It
  </title>
</book>

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.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}