0

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

Grails 3.2.5.

Jay
  • 173
  • 10

2 Answers2

0

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 )
    ...
}
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
0

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 ]
}
injecteer
  • 20,038
  • 4
  • 45
  • 89