Grails render as JSON catch

One of a reasons your controller doesn’t render a proper response in JSON format might be wrong package name that you use. It is easy to overlook. Import are on top of a file, you look at your code and everything seems to be fine. Except response is still not in JSON format. Consider this simple controller: class RestJsonCatchController {def grailsJson() {render([first: 'foo', second: 5] as grails.converters.JSON) }def netSfJson() {render([first: 'foo', second: 5] as net.sf.json.JSON) }} And now, with finger crossed… We have a winner! $ curl localhost:8080/example/restJsonCatch/grailsJson{"first":"foo","second":5}$ curl localhost:8080/example/restJsonCatch/netSfJson{first=foo, second=5} As you can see only grails.converters.JSON converts your response to JSON format. There is no such converter for net.sf.json.JSON, so Grails has no converter to apply and it renders Map normally. Conclusion: always carefully look at your imports if you’re working with JSON in Grails! Edit: Burt suggested that this is a bug. I’ve submitted JIRA issue here: GRAILS-9622 render as class that is not a codec should throw exception

One of a reasons your controller doesn’t render a proper response in JSON format might be wrong package name that you use. It is easy to overlook. Import are on top of a file, you look at your code and everything seems to be fine. Except response is still not in JSON format.

Consider this simple controller:

<span class="keyword">class</span> <span class="class">RestJsonCatchController</span> {<br />    <span class="keyword">def</span> <span class="method">grailsJson</span>() {<br />        <span class="method">render</span>([<span class="field">first</span>: <span class="string">'foo'</span>, <span class="field">second</span>: <span class="number">5</span>] <span class="keyword">as</span> grails.converters.<span class="class">JSON</span>)<br />    }<br /><br />    <span class="keyword">def</span> <span class="method">netSfJson</span>() {<br />        <span class="method">render</span>([<span class="field">first</span>: <span class="string">'foo'</span>, <span class="field">second</span>: <span class="number">5</span>] <span class="keyword">as</span> net.sf.json.<span class="class">JSON</span>)<br />    }<br />}<br />

And now, with finger crossed… We have a winner!

<span class="keyword">$</span> curl localhost:8080/example/restJsonCatch/grailsJson<br />{"first":"foo","second":5}<br /><span class="keyword">$</span> curl localhost:8080/example/restJsonCatch/netSfJson<br />{first=foo, second=5}<br />

As you can see only grails.converters.<span class="class">JSON</span> converts your response to JSON format. There is no such converter for net.sf.json.<span class="class">JSON</span>, so Grails has no converter to apply and it renders Map normally.

Conclusion: always carefully look at your imports if you’re working with JSON in Grails!

Edit: Burt suggested that this is a bug. I’ve submitted JIRA issue here: GRAILS-9622 render as class that is not a codec should throw exception

You May Also Like

New HTTP Logger Grails plugin

I've wrote a new Grails plugin - httplogger. It logs:

  • request information (url, headers, cookies, method, body),
  • grails dispatch information (controller, action, parameters),
  • response information (elapsed time and body).

It is mostly useful for logging your REST traffic. Full HTTP web pages can be huge to log and generally waste your space. I suggest to map all of your REST controllers with the same path in UrlMappings, e.g. /rest/ and configure this plugin with this path.

Here is some simple output just to give you a taste of it.

17:16:00,331 INFO  filters.LogRawRequestInfoFilter  - 17:16:00,340 INFO  filters.LogRawRequestInfoFilter  - 17:16:00,342 INFO  filters.LogGrailsUrlsInfoFilter  - 17:16:00,731 INFO  filters.LogOutputResponseFilter  - >> #1 returned 200, took 405 ms.
17:16:00,745 INFO filters.LogOutputResponseFilter - >> #1 responded with '{count:0}'
17:18:55,799 INFO  filters.LogRawRequestInfoFilter  - 17:18:55,799 INFO  filters.LogRawRequestInfoFilter  - 17:18:55,800 INFO  filters.LogRawRequestInfoFilter  - 17:18:55,801 INFO  filters.LogOutputResponseFilter  - >> #2 returned 404, took 3 ms.
17:18:55,802 INFO filters.LogOutputResponseFilter - >> #2 responded with ''

Official plugin information can be found on Grails plugins website here: http://grails.org/plugins/httplogger or you can browse code on github: TouK/grails-httplogger.