网站首页 > 技术文章 正文
前言
在开发项目时,压缩响应体数据包大小也是我们常用的优化手段之一。如果响应体过大,会浪费网络流量,增加接口延迟。
SpringBoot支持gzip压缩,并且支持自动压缩,即响应内容长度较短时不压缩,保证响应速度,响应内容长度较长时压缩,减轻带宽压力,配置极其简单。
HTTP响应的gzip压缩
当服务端对响应做了gzip压缩后,header里会添加:Content-Encoding: gzip用于表明返回结果做了gzip压缩。如果客户端不希望该次请求会被压缩,则可以修改请求的header中的Accept-Encoding,去掉gzip即可
springboot配置
springboot压缩配置如下:
server:
  port: 8081
  compression: #开启gzip压缩,返回内容大于2k的才会进行压缩
    enabled: true
    mime-types: application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plain
    min-response-size: 2048
  tomcat:
    max-threads: 300
    accept-count: 300
    min-spare-threads: 50
    max-connections: 15000
    connection-timeout: 60000FilterConfig 配置
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingResponseWrapper;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterBean = new FilterRegistrationBean();
        filterBean.setFilter(new AddContentLengthFilter());
        filterBean.setUrlPatterns(Arrays.asList("*"));
        return filterBean;
    }
    class AddContentLengthFilter extends OncePerRequestFilter {
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            ContentCachingResponseWrapper cacheResponseWrapper;
            if (!(response instanceof ContentCachingResponseWrapper)) {
                cacheResponseWrapper = new ContentCachingResponseWrapper(response);
            } else {
                cacheResponseWrapper = (ContentCachingResponseWrapper) response;
            }
            filterChain.doFilter(request, cacheResponseWrapper);
            cacheResponseWrapper.copyBodyToResponse();
        }
    }
}这里添加了一个Filter,在这个Filter中,使用了ContentCachingResponseWrapper包装response。ContentCachingResponseWrapper会缓存所有写给OutputStream的数据,并且因为缓存了内容,所以可以获取Content-Length并帮忙设置。
测试
使用Postman 模拟请求,查询系统配置接口,未压缩前请求体 17K左右,请求如下:
增加压缩配置后,请求体只有 3.5K左右。
猜你喜欢
- 2024-12-25 Spring Boot整合Spring Cloud GateWay代理第三方应用的调用接口?
 - 2024-12-25 Java 近期新闻:Hibernate 6.0、JobRunr 5.0、JHipster 7.8.0
 - 2024-12-25 Keycloak Servlet Filter Adapter使用
 - 2024-12-25 如何在Spring Boot中保证RESTful接口的安全性?
 - 2024-12-25 Java项目实战第6天:登录业务的实现
 - 2024-12-25 JavaEE概述总结:Servlet生命周期+JSP内置对象
 - 2024-12-25 SpringBoot 无感刷新 Token springboot的token
 - 2024-12-25 若依开发框架解析笔记(7)-jwt的应用
 - 2024-12-25 Spring MVC中提供了哪些扩展机制?如何使用这些扩展机制?
 - 2024-12-25 49个Spring经典面试题总结(附带答案)
 
- 最近发表
 - 
- 聊一下 gRPC 的 C++ 异步编程_grpc 异步流模式
 - [原创首发]安全日志管理中心实战(3)——开源NIDS之suricata部署
 - 超详细手把手搭建在ubuntu系统的FFmpeg环境
 - Nginx运维之路(Docker多段构建新版本并增加第三方模
 - 92.1K小星星,一款开源免费的远程桌面,让你告别付费远程控制!
 - Go 人脸识别教程_piwigo人脸识别
 - 安卓手机安装Termux——搭建移动服务器
 - ubuntu 安装开发环境(c/c++ 15)_ubuntu安装c++编译器
 - Rust开发环境搭建指南:从安装到镜像配置的零坑实践
 - Windows系统安装VirtualBox构造本地Linux开发环境
 
 
- 标签列表
 - 
- cmd/c (90)
 - c++中::是什么意思 (84)
 - 标签用于 (71)
 - 主键只能有一个吗 (77)
 - c#console.writeline不显示 (95)
 - pythoncase语句 (88)
 - es6includes (74)
 - sqlset (76)
 - apt-getinstall-y (100)
 - node_modules怎么生成 (87)
 - chromepost (71)
 - flexdirection (73)
 - c++int转char (80)
 - mysqlany_value (79)
 - static函数和普通函数 (84)
 - el-date-picker开始日期早于结束日期 (76)
 - js判断是否是json字符串 (75)
 - c语言min函数头文件 (77)
 - asynccallback (87)
 - localstorage.removeitem (77)
 - vector线程安全吗 (73)
 - java (73)
 - js数组插入 (83)
 - mac安装java (72)
 - 无效的列索引 (74)
 
 
