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

As a Response to an Ajax request, I would like to send pressed i.e. gzipped JSON from my Java program.

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 badges
Add a ment  | 

3 Answers 3

Reset to default 1

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.

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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信