Grails 3 how to render json view to file output instead of to http response stream?

301 Views Asked by At

In a controller, how can I redirect json view output to a file instead of to the http response?

Grails 3.2.5.

2

There are 2 best solutions below

0
On

Another simple option would be:

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

You can do something like this...

@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 )
    ...
}