0
votes

I'm using a domain object to interface with a database in Grails.

When I use the list() method on a domain object to get all of the rows from a database it works great except for one thing. The object that comes back for each row also includes an attribute called "class". I've read some things about creating a custom marshaller that would allow me to remove that attribute from the object. Is that really the best way to not have to return the class attribute?

Thanks!

2
Are you seeing the class in the actual database record or are you talking about when you render it as JSON? - Joshua Moore

2 Answers

0
votes

You can also use JSON.registerObjectMarshaller as below:

// BootStrap.groovy
JSON.registerObjectMarshaller( YourDomain ) {
    it.properties.findAll { it.name != 'class' }
}

Refer here for a similar example.

0
votes

Here's a link to change the way Grails renders JSON by default: http://grails.org/doc/2.4.4/guide/single.html#defaultRenderers

Just change "NameOfDomainClass" to the class you want to render differently. In this case, the Domain Class.

import grails.rest.render.json.*
beans = {
    bookRenderer(JsonRenderer, NameOfClass) {
        excludes = ['class']
    }
}