Spring Boot - Spring Boot 静态资源延迟响应(使用拦截器、使用过滤器、使用 ResourceResolver)

需求概述

yaml 复制代码
spring:
  mvc:
    static-path-pattern: /file/download/**
  web:
    resources:
      static-locations: file:D:/files/
  • 在 Spring Boot 中,在上述静态资源映射的基础上,实现延迟响应,用于测试请求超时

一、使用拦截器

java 复制代码
@Component
public class DelayInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 对静态资源请求进行延迟
        if (handler instanceof ResourceHttpRequestHandler) {
            Thread.sleep(20 * 1000);
        }

        return true;
    }
}
java 复制代码
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private DelayInterceptor delayInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 配置拦截器
        registry.addInterceptor(delayInterceptor)
                .addPathPatterns("/file/download/**");
    }
}

二、使用过滤器

java 复制代码
@Component
@Order(1)
public class DelayFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;

        String path = httpRequest.getRequestURI();

        if (path.startsWith("/file/download/")) {
            try {
                Thread.sleep(20 * 1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }
}

三、使用 ResourceResolver

java 复制代码
public class DelayResourceResolver extends PathResourceResolver {

    @Override
    protected Resource getResource(String resourcePath, Resource location) throws IOException {
        
        // 对静态资源请求进行延迟
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return super.getResource(resourcePath, location);
    }
}
java 复制代码
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        // 配置 ResourceResolver
        registry.addResourceHandler("/file/download/**")
                .addResourceLocations("file:D:/files/")
                .resourceChain(true)
                .addResolver(new DelayResourceResolver());
    }
}
  • 注:有了上述 WebConfig 配置类,就不需要在 application.yaml 配置文件中配置相关的静态资源映射了
相关推荐
AAA修煤气灶刘哥2 分钟前
两个 Agent 各自改得都对,合在一起为什么出错?
人工智能·后端·算法
Cosolar3 分钟前
概率性计算的系统工程:从 Super-agent 与 Octop 看多 Agent 架构的收敛
人工智能·后端·架构
bai2735 分钟前
npm 在 cmd 里能跑,Node 的 spawn 一调就 EINVAL:Windows 上 .cmd 的六种调法实测
javascript·后端
挖掘狂人38 分钟前
如何保证缓存与数据库一致性
数据库·后端·程序员
SimonKing41 分钟前
别再自己写同步脚本了!这款开源中间件,一套界面搞定 22 种数据源
java·后端·程序员
星落梦生1 小时前
苍穹外卖day3——阿里云oss文件上传
java·开发语言
头发还在的女程序员1 小时前
Spring Boot云快充协议接入:Netty报文分帧、长度检查与校验处理设计
java·spring boot·后端·充电桩·充电桩小程序·云快充
captain3761 小时前
SpringCloud(12)-Gateway
spring boot·spring cloud·gateway
جيون داد ناالام ميづ2 小时前
三、MyBatis 与 Spring Boot 集成:极简配置 + 分页 / 乐观锁实战
spring boot·后端·mybatis
行者-全栈开发2 小时前
SpringBoot API 网关实战:Spring Cloud Gateway 路由/限流/熔断/鉴权一体化方案
spring boot·后端·adb