问题
My Grails 1.3.7 controller is generating some JSON & I would like to send it to the client in compressed (zipped) form, as the amount of JSON can be 2+MB uncompressed. The resources plugin I found seems to compress static resources only. Is there a way to compress dynamic controller output as well?
回答1:
I guess you would need the below configuration in Config.groovy
if Ui-Performance plugin is used:
uiperformance.html.includeContentTypes = ['text/json','application/json']
Also have a look a CompressingFilter API which is used to compress HttpServletResponse. If curious, you can also peep at the source as to how the web descriptor is manipulated to use the compressor.
回答2:
If you're serving your grails application from behind a web-server like apache or nginx, you can configure the compression in the front-end rather easily.
See mod_deflate for apache and HttpGzipModule for nginx.
If you would rather do the compression work inside your jar, you can either create a ServletFilter
and do the compression yourself as mentioned in this SO question or use something like the UI-Performance plugin as mentioned by dmahapatro.
来源:https://stackoverflow.com/questions/17309909/how-to-compress-output-from-a-grails-controller