Grails 2 - 自动生成JSON输出(就像Spring 3.x一样) [英] Grails 2 - produce JSON output automatically (like Spring 3.x does)

查看:94
本文介绍了Grails 2 - 自动生成JSON输出(就像Spring 3.x一样)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring MVC 3.x中,我可以配置一个 ContentNegotiatingViewResolver bean,以使用JSON或XML自动呈现任何给定的端点,只需将文件扩展名更改为 .json .xml 。我认为在Grails中有一个相同的功能,但我找不到它。



我读过的所有内容都说明了我必须捕获传入的MIME类型(使用 withFormat ),然后在每个控制器方法中使用渲染为JSON (或等价物)指定JSON输出。 用Grails渲染JSON?)。在我潜入并开始添加特定于JSON的代码到我的控制器之前,我想我会问这里...

所以我的问题是:我可以配置Grails 2通过简单地为任何给定的URL添加`.json'文件扩展名(或更改接受标头),自动生成JSON输出? 解决方案

我认为您可以使用 grails过滤器轻松地使用它

这是一个过滤器,我在矿山应用程序中完成了OAuth API,它根据接受头文件执行xml,json和yalm

 class RenderFilters {

def grailsApplication

def filters = {

multiFormat(controller:'* EndPoint' ,action:'*',搜索:true){

after = {地图模型 - >
$ b def accept = request.getHeaders('accept')*。toLowerCase()

def out = model.containsKey('ou​​t')?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)返回true
$ b $ if 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)







$ $ $ $ pre $ $ $ b

In Spring MVC 3.x I can configure a ContentNegotiatingViewResolver bean to automatically render any given endpoint in either JSON or XML simply by changing the file extension to .json or .xml. I assumed there was an equivalent functionality in Grails but I can't find it.

Everything I've read says I have to catch the incoming mime-type (using withFormat) and then specify the JSON output using render as JSON (or equivalent) in every one 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 produce JSON output by simply adding a `.json' file extension (or changing the accept header) for any given URL?

解决方案

I think you can easly to it using a grails filter

This is a filter I have done ab OAuth API in a mine application, it do 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
                }

        }

    }
}

这篇关于Grails 2 - 自动生成JSON输出(就像Spring 3.x一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆