2012-08-09 26 views
7

在Spring MVC 3.x中,我可以配置一個ContentNegotiatingViewResolver bean,以便只需將文件擴展名更改爲.json即可自動呈現JSON或XML中的任何給定端點,或者.xml。我認爲在Grails中有相當的功能,但我找不到它。Grails 2 - 自動生成JSON輸出(與Spring 3.x一樣)

一切我讀過說,我要趕(使用withFormat)傳入的MIME類型,然後指定在我的控制方法,每一個(例如rendering JSON with Grails?)使用render as JSON(或同等學歷)的JSON輸出。在我潛入並開始添加JSON特定的代碼到我的控制器之前,我想我會問這裏...

所以我的問題是:我可以配置Grails 2自動生成JSON輸出,只需添加一個`.json '文件擴展名(或更改接受標題)的任何給定的網址?

+1

如果您仍然在使用腳手架,您可以將其添加到腳手架上,它將應用於您的所有控制器。 – cdeszaq 2012-08-09 13:20:15

回答

7

我使用覺得你可以伊斯利給它的grails filter

這是一個過濾器我已經做了AB 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 
       } 

     } 

    } 
} 
+0

這看起來像要走的路。我會在星期一試一試,然後再接受是正確的。它甚至具有允許我添加JSONP回調包裝的優勢! – nickdos 2012-08-11 05:55:15

3

對於任何遇到這個SO問題的人,我想我會包含我的最終Grails(2.x版)過濾代碼,因爲它與Fabiano的答案(上面)不同。

以下濾波器允許普通的HTML內容爲通過正常的Grails來處理,並使用Grails的內容協商機制由文件擴展名來設置response.format或接受報頭(取決於CONF設置:grails.mime.use.accept.header & grails.mime.file.extensions)。我還添加了對JSONP回調包裝器的支持。

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 
       } 
      } 
     } 
    } 
} 
2

偶然,我發現最新的grails會自動輸出JSON和XML,只需在查詢中設置Accept標頭即可!

我使用2.3.2此刻卻可能是早期版本相同的作品,我只是創建了一個新的應用程序,創建了一個新的簡單的域類的一些屬性,跑產生-all,然後運行應用程序。運行後,curl -i -H「Accept:application/json」返回JSON和curl -i -H「Accept:application/xml」返回XML,無需任何額外工作。

我是這麼驚訝,這保證我還沒有安裝一些奇怪的事情在我的本地機器上,我有一個新的Grails安裝試了一個全新的服務器上工作得很好!