为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

阿波罗的战车 2022-12-28 15:38:58
我想在我的 Spring Boot 应用程序中为匹配/async/*. 例子:localhost:8080/async/downloadLargeFilelocalhost:8080/async/longRunningRask以第一个例子为例,我使用如下方法实现了我的方法StreamingResponseBody:@GetMappingpublic 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了:@Configurationpublic 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只适用于匹配模式的请求子集?如果这不可能或不推荐,那么完成相同行为的正确方法是什么?
查看完整描述

1 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

TaskExecutor为/与WebMvcConfigureron配置时,AsyncSupportConfigurer将仅用于 Web 请求的异步处理。所有其他请求都由您的 servlet 容器上可用的默认请求处理线程处理。

异步性质由方法的返回类型定义。异步类型在 Spring 参考指南的MVC 异步部分进行了描述。


查看完整回答
反对 回复 2022-12-28
  • 1 回答
  • 0 关注
  • 81 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号