[英]GZip compression of REST response
I am writing a REST service where I am sending a JSON response of size around 3MB. 我正在编写REST服务,在其中发送大小约为3MB的JSON响应。 Application is developed using Scalatra Framework and running on Tomcat server.
应用程序是使用Scalatra Framework开发的,并在Tomcat服务器上运行。 As data is big of size, I want to zip the content before sending over the network to browser client.
由于数据量很大,因此我想先压缩内容,然后再通过网络发送到浏览器客户端。
To compress the response, I have added below code in tomcat server.xml file: 为了压缩响应,我在tomcat server.xml文件中添加了以下代码:
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
compression="on"
compressionMinSize="100"
noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml,text/json,text/javascript,text/css,text/plain, application/javascript,application/xml,application/xml+xhtml,application/json"/>
But I didn't find any different in the content transferred before and after adding the above configuration. 但是在添加上述配置之前和之后,我发现传输的内容没有任何不同。 It is adding content-encoding header Content-Encoding:gzip .
它正在添加内容编码标头Content-Encoding:gzip 。 It didn't solve my actual goal.
它没有解决我的实际目标。
To test whether this configuration is really working or not, I have copied JSON file into server and tried to access that file. 为了测试此配置是否真的有效,我已将JSON文件复制到服务器并尝试访问该文件。 It is being received as small compressed file in the client side.
它在客户端作为小型压缩文件被接收。
Seems I am missing some configuration to add to make HTTP JSON response as zipped one. 似乎我缺少一些配置以使HTTP JSON响应压缩。 Can someone help me to solve this problem?
有人可以帮我解决这个问题吗?
I was able to solve this problem by using GzipFilter from ehcache-web dependency. 通过使用ehcache-web依赖项中的GzipFilter ,我能够解决此问题。
Added below dependencies in build.scala : 在build.scala中添加了以下依赖项:
"org.slf4j" % "slf4j-jdk14" % "1.6.4",
"net.sf.ehcache" % "ehcache-web" % "2.0.4",
Filter mapping in web.xml web.xml中的过滤器映射
<filter>
<filter-class>net.sf.ehcache.constructs.web.filter.GzipFilter</filter-class>
<filter-name>GzipFilter</filter-name>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>
Above code changes has solved my problem. 上面的代码更改解决了我的问题。 Now response is compressed.
现在响应已压缩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.