2

I'm using Grails 2.3.2 working on a REST API using the built in Grails REST support. I'm having trouble getting rid of the "class" element in the JSON response. Based on a tutorial by Bobby Warner, I have found adding the following to the resources.groovy file:

meterRenderer(JsonRenderer, Meter) {
    excludes = ['class']
}

This works fine for show, but for the index controller function, I'm responding with a list of Meters. In this, the "class" doesn't go away. What does it take to get rid of this in the list response?

Edit: To clarify, I am looking for a way to leverage the Content Negotiation feature of Grails new respond functionality without locking myself down to render as JSON implementions.

chenj7
  • 95
  • 1
  • 10
  • 1
    Have a look at http://stackoverflow.com/questions/6495633/grails-grails-converters-json-removing-the-class-name – nickdos Feb 04 '14 at 02:48
  • Thanks, nickdos. I am new to Grails REST, so please take a look at my clarification edit and tell me if it makes no sense. – chenj7 Feb 04 '14 at 20:25

2 Answers2

1

I guess if you switch to using GSON (github) instead of JSON then you need not worry about that particular exclusion.

That is driven by a config setting provided by the plugin as grails.converters.gson.domain.include.class (default is false).

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • Thanks, dmahapatro. The GSON plugin looks awesome, but it seems that its RESTful controller support as it stands simply overwrites the existing controller scaffold with hard coded `render as GSON` responses rather than updating `respond` functionality. I have edited my answer to hopefully clarify what I'm looking for. – chenj7 Feb 04 '14 at 20:44
0

nickdos' SO link had the answer. I added the following to my BootStrap.groovy:

grails.converters.JSON.registerObjectMarshaller(Meter) {
    return it.properties.findAll {k,v -> k != 'class'}
}

And the respond call results in no "class" item. Oddly enough, I lost the "id" item in the process, but I'll save that for another SO question. :)

Community
  • 1
  • 1
chenj7
  • 95
  • 1
  • 10