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 配置文件中配置相关的静态资源映射了
相关推荐
苦瓜小生12 分钟前
【前端】|【js手撕】经典高频面试题:手写实现function.call、apply、bind
java·前端·javascript
NGC_66111 小时前
Java 线程池:execute () 和 submit () 到底有什么区别?
java
cngm1101 小时前
解决麒麟v10下tomcat无法自动启动的问题
java·tomcat
色空大师1 小时前
【网站搭建实操(一)环境部署】
java·linux·数据库·mysql·网站搭建
客卿1231 小时前
牛客刷题--找数字-- 字符串检测-字符串 双指针
java·开发语言
二进制person1 小时前
JavaEE进阶 --Spring Framework、Spring Boot和Spring Web MVC(2)
spring boot·spring·java-ee
烛之武1 小时前
SpringBoot基础
java·spring boot·后端
橙序员小站1 小时前
Harness Engineering:从 OpenClaw 看 AI 助理的基础设施建设
后端·aigc·openai
Amour恋空2 小时前
Java多线程
java·开发语言·python
小胖java2 小时前
高校培养方案制定系统
java·spring