1

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 Answer 1

1

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'
Sign up to request clarification or add additional context in comments.

1 Comment

Im going to accept your answer even though i find it smelly to put stuff like this in the bootstrap file. I ended up following this guide. compiledammit.com/2012/08/16/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.