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

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

+5
source share
3 answers

Thank you guys for your submissions. I used the following to make it work.

The following filter has been added to my web.xml application:

<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 servlet.xml the following bean property has been added for the DataViewController bean.

 <beans:property name="contentType" value="application/json" /> 
+1
source

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

+1
source

Your server-side code should also have a gzip response, in addition to setting the content encoding header. You can take a look at GZIPResponseWrapper.java .

0
source

All Articles