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 } } } }
Fabiano taioli
source share