1
votes

In my Grails-app I have two domains Person and County

class Person {
     String firstName
     String lastName
     County county
     LocalDate dateOfBirth
     static hasMany = [episodes: Episode]
 }
 class County {
     String name
     // other stuff...
 }

When trying to render my List of persons from my controller i only get County { class: County, id: 1 } not the name of County. I want the properties of objects related to my Person.

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond Person.list(params), model:[personInstanceCount: Person.count()]
}

I don't want to default to the deep converter, then my belongsTo, and hasMany relations don't seem to work.

grails.converters.json.default.deep = true

I have tried and failed with customRenderers, Grails does not care about any changes i do there.

 personRenderer(JsonRenderer, Person) {
        excludes = ['class']
    }
    personsRenderer(JsonCollectionRenderer , Person){
        excludes = ['class']
    }
    countyRenderer(JsonRenderer, County) {
        excludes = ['class']
        includes = ['name']
    }
    countiesRenderer(JsonCollectionRenderer , County){
        excludes = ['class']
        includes = ['name']
    }

I have tried with CustomMarshallerRegistrar same result as above, nothing happens at all, same result. Se 8.1.6.2 http://grails.org/doc/latest/guide/webServices.html#objectMarshallerInterface

So, how do i get my Person-objects to include the related County and not only the Class and ID properties?

Im using Grails 2.3.1 with jdk 1.7 on Windows

1

1 Answers

1
votes

if you are interested in responding it as JSON, you could try the following:

register object marshaller in the bootstrap

JSON.registerObjectMarshaller(Person) {
    def person = [:]
    ['firstName', 'lastName', 'country', 'dateOfBirth'].each { name ->
        person = it[name]
    }
    return person
}

JSON.registerObjectMarshaller(Country) {
    def country = [:]
    ['name'].each { name ->
        country = it[name]
    }
    return country
}

and then as a response from your controller..

render text: [personList:Person.list(params), personInstanceCount: Person.count()] as JSON, contentType: 'application/json'