[英]GZip compression of REST response
我正在編寫REST服務,在其中發送大小約為3MB的JSON響應。 應用程序是使用Scalatra Framework開發的,並在Tomcat服務器上運行。 由於數據量很大,因此我想先壓縮內容,然后再通過網絡發送到瀏覽器客戶端。
為了壓縮響應,我在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"/>
但是在添加上述配置之前和之后,我發現傳輸的內容沒有任何不同。 它正在添加內容編碼標頭Content-Encoding:gzip 。 它沒有解決我的實際目標。
為了測試此配置是否真的有效,我已將JSON文件復制到服務器並嘗試訪問該文件。 它在客戶端作為小型壓縮文件被接收。
似乎我缺少一些配置以使HTTP JSON響應壓縮。 有人可以幫我解決這個問題嗎?
通過使用ehcache-web依賴項中的GzipFilter ,我能夠解決此問題。
在build.scala中添加了以下依賴項:
"org.slf4j" % "slf4j-jdk14" % "1.6.4",
"net.sf.ehcache" % "ehcache-web" % "2.0.4",
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>
上面的代碼更改解決了我的問題。 現在響應已壓縮。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.