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 配置文件中配置相关的静态资源映射了
相关推荐
葫芦和十三3 小时前
图解 MongoDB 23|两地三中心:跨可用区部署怎么扛机房故障
后端·mongodb·agent
勇哥java实战分享4 小时前
PaddleOCR 太慢?我换成 RapidOCR 后,速度直接起飞
后端
苏三说技术9 小时前
LangChain4j 和 LangGraph4j,哪个更好?
后端
ServBay10 小时前
7 个AI开发中真正用得上的 MCP Server,配合Claude Code食用效果更佳
后端·claude·mcp
妙码生花10 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
用户67570498850211 小时前
Go 语言里判断字符串为空,90% 的人都写错了!
后端·go
Flittly11 小时前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
用户67570498850211 小时前
Go 进阶必修:90% 的人都没用对的“表驱动法”
后端·go
小兔崽子去哪了11 小时前
Java 生成二维码解决方案
java·后端