简体   繁体   English

在春季嵌入式tomcat中添加gzip

[英]Adding gzip to spring embedded tomcat

I'm trying to add gzip compression to my server (reading and writing) I've tried it with (spring boot): 我正在尝试将gzip压缩添加到我的服务器(读取和写入)中,并使用(弹簧启动)进行了尝试:

server.tomcat.compression=on
server.tomcat.compressableMimeTypes=application/json,application/xml,text/html,text/xml,text/plain

but when my client sends : 但是当我的客户发送时:

        GZIPOutputStream gzip = new GZIPOutputStream(baos);
        gzip.write(content.getBytes(Charset.forName("UTF8")));
        gzip.close();

with headers : 带标题:

        Accept-Encoding" - "gzip"
        "Content-Encoding" -  "gzip"

The data reaches the service still compressed. 数据到达仍被压缩的服务。 What did I missed? 我错过了什么?

Make sure your client is sending the correct Content-Encoding header, without it Tomcat won't know that decompression is needed. 确保您的客户端发送正确的Content-Encoding标头,否则,Tomcat将不知道需要解压缩。 See this answer for details . 有关详细信息,请参见此答案

I ended up using an existing filter : 我最终使用了现有的过滤器:

  <dependency>
        <groupId>net.sourceforge.pjl-comp-filter</groupId>
        <artifactId>pjl-comp-filter</artifactId>
        <version>1.7</version>
    </dependency>

And added a bean for it (new CompressingFilter()) 并为其添加了一个bean(新的CompressingFilter())

If anyone had the same problem or knows why it didn't work with the spring boot configuration I would love to know. 如果有人遇到相同的问题或知道为什么它不适用于Spring Boot配置,我很想知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM