猿问

将 Spring Boot WebMvcConfigurer 限制为仅指定路径

我想在我的 Spring Boot 应用程序中为匹配/async/*. 例子:

  • localhost:8080/async/downloadLargeFile

  • localhost:8080/async/longRunningRask

以第一个例子为例,我使用如下方法实现了我的方法StreamingResponseBody

@GetMapping

public ResponseEntity<StreamingResponseBody> downloadLargeFile() throws IOException {

    long size = Files.size(path);

    InputStream inputStream = Files.newInputStream(path);

    return ResponseEntity.ok()

        .contentLength(size)

        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=large_file.txt")

        .body(inputStream::transferTo);

}

在 的文档中StreamingResponseBody,它指出我应该配置一个AsyncTaskExecutor,所以我有这个 @Configuration 类也实现WebMvcConfigurer了:


@Configuration

public class AsyncConfigurer implements WebMvcConfigurer {


    @Override

    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {

        configurer.setDefaultTimeout(-1);

        configurer.setTaskExecutor(asyncTaskExecutor());

    }


    @Bean

    public AsyncTaskExecutor asyncTaskExecutor() {

        return new SimpleAsyncTaskExecutor("async");

    }

}

但是,我找不到仅对匹配给定模式的请求使用此任务执行器的方法。


作为一个更一般的问题 -我如何限制WebMvcConfigurer只适用于匹配模式的请求子集?


如果这不可能或不推荐,那么完成相同行为的正确方法是什么?


阿波罗的战车
浏览 82回答 1
1回答

Cats萌萌

当TaskExecutor为/与WebMvcConfigureron配置时,AsyncSupportConfigurer将仅用于 Web 请求的异步处理。所有其他请求都由您的 servlet 容器上可用的默认请求处理线程处理。异步性质由方法的返回类型定义。异步类型在 Spring 参考指南的MVC 异步部分进行了描述。
随时随地看视频慕课网APP

相关分类

Java
我要回答