使用thymeleaf在客户端处理streamingbodyresponse

crcmnpdw  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(298)

我用的是SpringBoot4.2。我有一个返回streamingbodyresponse(控制台输出)的服务。我想在一个视图中显示这个输出(最好是thymeleaf,但一切都很好。)现在它只是打开一个浏览器并在那里流式处理它。我想做的是在thymeleaf视图的文本区域中显示流式处理响应,并且关闭这个视图不应该影响流式处理停止和崩溃服务器端的事情。如果有人能给我指出正确的方向那就太好了。

@PostMapping(value="/environment",produces =MediaType.TEXT_PLAIN)
public StreamingResponseBody environmentSubmit(@ModelAttribute Environment environment) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String currentUserName = authentication.getName();
    Configuration configuration = Configuration.createFromPostParams(
            environment.getDriver(),
            environment.getVersion(),
            currentUserName);
    StreamingResponseBody response =executor.execute(configuration);
    return response;
iklwldmw

iklwldmw1#

您可以尝试以下操作以确保它异步运行且不超时:

@Configuration
public static class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(-1);
        configurer.setTaskExecutor(asyncTaskExecutor());
    }

    @Bean
    public AsyncTaskExecutor asyncTaskExecutor() {
        return new SimpleAsyncTaskExecutor("async");
    }

}

具有相同代码的外部源

相关问题