As a Response to an Ajax request, I would like to send pressed 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?
As a Response to an Ajax request, I would like to send pressed 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?
Share Improve this question asked May 25, 2015 at 6:28 shawnnyglumshawnnyglum 2214 silver badges16 bronze badges3 Answers
Reset to default 1You 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.
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" />
Your server side code should also gzip the response, apart from setting the content-encoding header. You can take a look at GZIPResponseWrapper.java.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744236866a4564512.html
评论列表(0条)