As example we will take a loot at a simple grails application with only two domain classes. The target is to customize the xml representation of the Person object. The implementation should be as simple as possible and easy to test. This is the basic structure of our domainclasses.
A call to the PersonController like http://localhost:8080/PetManager/person/show/1 produces a xml string like this.
<?xml version="1.0" encoding="UTF-8"?> <person id="1"> <lastName>Mueller</lastName> <birthday>2011-01-18 14:57:49.193 MEZ</birthday> <pets> <pet id="1" ></pet> <pet id="2" ></pet> </pets> <firstName>Michael</firstName> </person>
There are several parts that we would like to improve:
- better formatting of a date
- show the content of the pet objects instead of the reference
How we can make grails using a custom toXml() method is showed in part 2 of this article.