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

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. 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.

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}