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 配置文件中配置相关的静态资源映射了
相关推荐
zzqssliu5 分钟前
SpringBoot框架搭建跨境独立站|Taocarts代购系统订单模块深度开发
java·spring boot·后端
Loo国昌10 分钟前
从 Agent 编排到 Skill Runtime:企业 AI 工程化的下一层抽象
大数据·人工智能·后端·python·自然语言处理
dinl_vin19 分钟前
FastAPI 系列 ·(四):数据库集成——SQLAlchemy 2.0 异步 ORM 与 Alembic 迁移
java·数据库·fastapi
小羊在睡觉20 分钟前
力扣239. 滑动窗口最大值
数据结构·后端·算法·leetcode·go
编码者卢布21 分钟前
【Azure Service Bus】Azure Service Bus Java SDK 中 Token 刷新异常的排查思路
java·python·azure
兰令水24 分钟前
topcode【随机算法题】【2026.5.20打卡-java版本】
java·开发语言·算法
AI瓦力35 分钟前
技术分享 | 彻底解决图片“躺平”问题:Java 后端强制校准图片方向
java
武子康38 分钟前
Java-219 RocketMQ Spring Boot 集成指南:生产者与消费者实战
java·spring boot·分布式·kafka·消息队列·rocketmq·java-rocketmq
RainCityLucky43 分钟前
Java Swing 自定义组件库分享(七)
java·笔记·后端
_Evan_Yao1 小时前
如何搭建属于自己的技术博客(CSDN / GitHub Pages)
后端·学习·github