【问题标题】:Grails 3 how to render json view to file output instead of to http response stream?Grails 3如何将json视图呈现到文件输出而不是http响应流?
【发布时间】:2018-02-19 16:54:34
【问题描述】:

在控制器中,如何将 json 视图输出重定向到文件而不是 http 响应?

Grails 3.2.5.

【问题讨论】:

    标签: json grails grails-controller grails-3.2


    【解决方案1】:

    你可以做这样的事情......

    @Autowired
    JsonViewTemplateEngine templateEngine
    
    void myMethod() {
        Template t = templateEngine.resolveTemplate('/book/show')
        def writable = t.make(book: new Book(title:"The Stand"))
        def fw = new FileWriter(...)
        writable.writeTo( fw )
        ...
    }
    

    【讨论】:

    【解决方案2】:

    另一个简单的选择是:

    def action() {
        def json = [ key1:'value1', key2:[ key21:'value21' ]
        new File( '/the/path' ).withOutputStream{ it << ( json as JSON ) }
        [ some:result ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-07
      • 2011-06-16
      • 1970-01-01
      • 2020-04-25
      • 2019-11-02
      • 1970-01-01
      • 2013-10-20
      • 2011-11-22
      相关资源
      最近更新 更多