How to compress output from a grails controller?

让人想犯罪 __ 提交于 2019-12-12 14:22:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!