如何从Java发送压缩(gzip)JSON作为对Ajax请求的响应?

时间:2021-04-01 20:49:38

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?

作为对Ajax请求的响应,我想从我的Java程序发送压缩的,即gzip压缩的JSON。我知道,我需要将响应头中的Content-Encoding设置为gzip,但这是我需要做的吗?

3 个解决方案

#1


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.

您还需要确保a)您的客户端(浏览器或应用程序)接受此gzip编码并可以处理它b)您的服务器(Java应用程序的容器)配置为默认发送gzip压缩响应。如果服务器配置为发送gzip压缩响应,则内容类型标头很可能由服务器本身设置。

#2


1  

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

谢谢你们的投入。我使用以下内容使其工作。

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

在我的应用程序web.xml中,添加了以下过滤器:

<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.

在servlet.xml中,将以下bean属性添加到DataViewController bean。

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

#3


0  

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

除了设置内容编码标头之外,您的服务器端代码还应该gzip响应。你可以看看GZIPResponseWrapper.java。

#1


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.

您还需要确保a)您的客户端(浏览器或应用程序)接受此gzip编码并可以处理它b)您的服务器(Java应用程序的容器)配置为默认发送gzip压缩响应。如果服务器配置为发送gzip压缩响应,则内容类型标头很可能由服务器本身设置。

#2


1  

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

谢谢你们的投入。我使用以下内容使其工作。

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

在我的应用程序web.xml中,添加了以下过滤器:

<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.

在servlet.xml中,将以下bean属性添加到DataViewController bean。

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

#3


0  

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

除了设置内容编码标头之外,您的服务器端代码还应该gzip响应。你可以看看GZIPResponseWrapper.java。