Java web application 使用 ziplet 来开启gzip压缩

目前除了 Spring 4 的 boot 和 Spring 5 里提供了相关应用层面的 Gzip 压缩,其他大部分时候都是通过容器(Tomcat、WAS、Jetty等)来开启 Gzip 压缩,剩下的都是 out of date 的自己写一个 filter 来实现这个功能。这里介绍一个开源的第三方库来实现这个功能。

加入依赖

使用 maven 导入依赖。

<!-- https://mvnrepository.com/artifact/com.github.ziplet/ziplet -->
<dependency>
    <groupId>com.github.ziplet</groupId>
    <artifactId>ziplet</artifactId>
    <version>2.1.2</version>
</dependency>

其他方式可以在 mvnrepository 找到。

增加 filter 配置

在 web.xml 中增加配置。

    <filter>
        <filter-name>CompressingFilter</filter-name>
        <filter-class>com.github.ziplet.filter.compression.CompressingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CompressingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

配置项

示例。

    <filter>
        <filter-name>CompressingFilter</filter-name>
        <filter-class>com.github.ziplet.filter.compression.CompressingFilter</filter-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

debug (optional): 是否输出 debug 信息,默认 false 。

compressionThreshold (optional): 设置开启压缩的最小 response 大小,单位 byte 。设置为 0 时永远开启压缩。默认 1024 。

statsEnabled (optional): 是否统计,详情见 CompressingFilterStats 。

includeContentTypes (optional): 设置针对某种 content type 开启压缩,如 text/html 。在 response 设置了 content type 之后调用。

excludeContentTypes (optional): include 和 exclude 是反义词,所以是设置排除某些 content type 的压缩。

includePathPatterns (optional): 类似 includeContentTypes ,不过是针对的 uri 。支持正则表达式,如 '.static.' 。

excludePathPatterns (optional): 排除特定 uri 的压缩。

includeUserAgentPatterns (optional): 针对特定 UA 的压缩。

excludeUserAgentPatterns (optional): 排除特定 UA 的压缩。

noVaryHeaderPatterns (optional): 类似 includeUserAgentPatterns 。请求中符合配置的 UA 的响应不会包含 vary header 。

=> github.com/ziplet

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,083评论 19 139
  • API定义规范 本规范设计基于如下使用场景: 请求频率不是非常高:如果产品的使用周期内请求频率非常高,建议使用双通...
    有涯逐无涯阅读 2,769评论 0 6
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,989评论 6 342
  • 一、概念(载录于:http://www.cnblogs.com/EricaMIN1987_IT/p/3837436...
    yuantao123434阅读 8,515评论 6 152
  • 工作流程 一次HTTP操作称为一个事务,其工作过程可分为四步: 1)首先客户机与服务器需要建立连接。只要单击某个超...
    保川阅读 4,650评论 2 14