首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grails 2-自动生成JSON输出(像Spring3.x一样)

Grails 2-自动生成JSON输出(像Spring3.x一样)
EN

Stack Overflow用户
提问于 2012-08-09 11:55:09
回答 3查看 2K关注 0票数 7

在Spring MVC3.x中,我可以配置JSON,只需将文件扩展名改为.json.xml,即可自动呈现任何给定的ContentNegotiatingViewResolver端点。我以为Grails中有相同的功能,但我找不到它。

我读到的所有内容都表明我必须捕获传入的mime类型(使用withFormat),然后在我的每个控制器方法(例如rendering JSON with Grails?)中使用render as JSON (或等效方法)指定JSON输出。在我深入研究并开始向控制器添加特定于JSON的代码之前,我想我应该在这里问一下……

所以我的问题是:我是否可以通过简单地为任何给定的URL添加一个`.json的文件扩展名(或更改accept头文件)来配置Grails 2以自动生成JSON输出?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-09 16:26:36

我想你使用grails filter就可以轻松地使用它。

这是我在一个矿山应用程序中的OAuth API中做的一个过滤器,它基于accept报头来做xml、json和yalm。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2012-08-13 12:13:03

对于遇到这个问题的任何人,我想我应该包括我的最终Grails (版本2.x)过滤器代码,因为它与Fabiano的答案(上面)有所不同。

下面的过滤器允许Grails照常处理普通的HTML内容,并使用Grails内容协商机制通过文件扩展名或accept头来设置response.format (取决于conf设置:grails.mime.use.accept.header & grails.mime.file.extensions)。我还添加了对JSONP回调包装器的支持。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-11-19 07:47:42

偶然间,我发现最新的grails只需在查询上设置Accept标头,就可以自动输出JSON和XML!

我目前使用的是2.3.2版本,但可能对早期版本也是如此,我只是创建了一个新的应用程序,创建了一个带有一些属性的新的简单域类,运行generate-all,然后运行run-app。运行之后,curl应用程序-H "Accept:-i / JSON“返回JSON,curl -i -H "Accept: application/ XML”返回XML,不需要任何额外的工作。

我对此感到非常惊讶,为了确保我没有在本地机器上安装任何奇怪的东西,我在安装了新grails的全新服务器上进行了尝试……它起作用了!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11876570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档