我想在我的 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只适用于匹配模式的请求子集?
如果这不可能或不推荐,那么完成相同行为的正确方法是什么?
Cats萌萌
相关分类