Grails 2 - Automatically outputs JSON output (e.g. Spring 3.x)

In Spring MVC 3.x, I can configure the ContentNegotiatingViewResolver bean to automatically display any endpoint in JSON or XML simply by changing the file extension to .json or .xml . I suggested that Grails has equivalent functionality, but I cannot find it.

All I read says I need to catch the incoming mime type (using withFormat ) and then specify the JSON output using render as JSON (or equivalent) in each of my controller methods (e.g. rendering JSON with Grails? ). Before I dive in and start adding JSON-specific code to my controllers, I thought I'd ask here ...

So my question is: can I configure Grails 2 to automatically generate JSON output by simply adding the .json file extension (or changing the accept header) for any given URL?

+7
source share
3 answers

I think you can easily use this grails filter

This is the filter that I executed ab OAuth API in my application, it is xml, json and yalm based on accept headers

 class RenderFilters { def grailsApplication def filters = { multiFormat(controller: '*EndPoint', action: '*', search: true) { after = { Map model -> def accepts = request.getHeaders('accept')*.toLowerCase() def out = model.containsKey('out')?model.out:model if(accepts.any{ it.contains('json') }){ render(text: out as JSON, contentType: 'application/json', encoding:"UTF-8") } else if(accepts.any{ it.contains('yaml') }){ render(text: Yaml.dump(out), contentType: 'application/x-yaml;', encoding:"UTF-8") } else if(accepts.any{ it.contains('html') }){ render(text: out as JSON, contentType: 'application/json', encoding:"UTF-8") } else if(accepts.any{ it.contains('xml') }){ render(text: out as XML, contentType: 'application/xml', encoding:"UTF-8") } else { render(text: out as JSON, contentType: 'application/json', encoding:"UTF-8") } false } before = { def contentType = request.getHeader('Content-Type')?.toLowerCase() if(!contentType) return true if(contentType == 'application/json'){ params.body = JSON.parse(request.reader) } if(contentType == 'application/xml'){ params.body = XML.parse(request.reader) } if(contentType == 'application/x-yaml'){ params.body = Yaml.load(request.reader) } params.body = new TypeConvertingMap((Map) params.body) true } } } } 
+7
source

For those who have come across this SO question, I thought I would include my latest Grails filter code (version 2.x), as it differs from Fabiano's answer (see above).

The following filter allows you to process regular HTML content as usual with Grails and uses the Grails content matching mechanism to set response.format to the file extension or accept a header (depending on the conf settings: grails.mime.use.accept.header and grails.mime.file.extensions ). I also added support for JSONP callback wrappers.

 import grails.converters.JSON import grails.converters.XML class RenderFilters { def filters = { multiFormat(controller: '*', action: '*', find: true) { after = { Map model -> def out = model?.containsKey('out')?model.out:model if (response.format == "json" && params.callback) { render(text: params.callback + "(${out as JSON})" , contentType: 'application/javascript', encoding:"UTF-8") false } else if (response.format == "json") { render(text: out as JSON, contentType: 'application/json', encoding:"UTF-8") false } else if (response.format == "xml") { render(text: out as XML, contentType: 'application/xml', encoding:"UTF-8") false } } } } } 
+3
source

By chance, I found that the last graals automatically output JSON and XML, just setting the Accept header in the request!

I am using 2.3.2 at the moment, but probably the same thing works for earlier versions, and I just created a new application, created a new simple domain class with some properties, launched everything, then launched the application. After running curl -i -H "Accept: application / json" returns JSON and curl -i -H "Accept: application / xml" returns XML without additional work.

I was so surprised by this that to make sure that I did not install something strange on my local machine, I tried it on a completely new server with a new installation of Grails ... and it works !!!

+2
source

All Articles