Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send compressed (gzip) JSON as response to an Ajax Request, from Java?

As a Response to an Ajax request, I would like to send compressed i.e. gzipped JSON from my Java program. I know, I would need to set the Content-Encoding in the Response Header to gzip, but is that all I need to do?

like image 543
sidrocks Avatar asked Nov 01 '22 03:11

sidrocks


2 Answers

You would also need to make sure that a) your client (browser or app) accepts this gzip encoding and can deal with it b) your server (container for your java application) is configured to send responses gzipped by default. If the server is configured to send gzipped responses, then the content-type header will most likely be set by the server itself.

like image 150
Farhad Avatar answered Nov 02 '22 23:11

Farhad


Thanks guys for your inputs. I used the following to make it work.

In my application web.xml, added the following filter:

<filter>
    <filter-name>GZipFilter</filter-name>
    <filter-class> org.mortbay.servlet.GzipFilter</filter-class>
    <init-param>
        <param-name>mimeTypes</param-name>
        <param-value>application/json</param-value>
    </init-param>
</filter>

<filter-mapping>    
    <filter-name>GZipFilter</filter-name>
    <url-pattern>*.data</url-pattern>
</filter-mapping>

And in the servlet.xml added the following bean property to DataViewController bean.

<beans:property name="contentType" value="application/json" />
like image 30
sidrocks Avatar answered Nov 02 '22 23:11

sidrocks