Question

Currently I'm developing a RESTFull using Grails. I use the findAll method to retrieve specific data:

def query = PlannedEvent.where{}
query = query.where{rooms.id == i }
respond query.findAll()

and the respond method to generate output:

{"class":"mp.ra.PlannedEvent","id":480,"baseEvent":{"class":"Event","id":1636},"equipment":[],"rooms":[{"class":"Room","id":22}]}

How can I alter the result so more information is given about the baseEvent and the Room.

The class is set up as followed:

class PlannedEvent {    
   Event baseEvent

   static hasMany = [rooms:Room,equipment:Resource]
   static belongsTo = [Room,Event,Resource]

   static constraints = {
      equipment nullable:true
   }
Was it helpful?

Solution

What you need is a custom JSON Marshaller for your PlannedEvent domain class. Basically you need to implement your own marshller then register it.

Here is an excellent blog post on how to implement your own custom JSON marshalling.

This way you can control exactly how everything gets marsheled into JSON.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top