grailsフィルターを使えば簡単にできると思います
これは、鉱山アプリケーションで OAuth API で行ったフィルターです。受け入れヘッダーに基づいて xml、json、および yalm を実行します。
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
}
}
}
}