小编CGX*_*106的帖子

如何在 Spring WebFlux 中的一个 Mono 中根据另一个请求执行连续的 Web 请求?

我想通过 Spring WebFlux 和给定的 REST-API 执行以下操作:

  1. 检索文件名列表 (GET /files)
  2. 删除每个检索到的文件(DELETE /files/local/{file name} for each)

问题是我不能将这两个操作组合到“一个”Mono 实例。我当前的实现是不够的,因为它阻止 Mono 实例立即执行 api 调用,而不是执行它们的反应性。

我的非反应式实现:

public Mono cleanUpUploadedFiles() {    
    WebClient webClient = this.getWebClient();

    // get all files / directories
    Mono<FilesOverview> filesOverviewMono = this.getResource("/files", FilesOverview.class);
    FilesOverview filesOverview = filesOverviewMono.block(); // TODO: prevent blocking mono

    // delete file / directory one by one
    for (FileOverview file : filesOverview.getFiles()) {
        ClientResponse clientResponse;

        clientResponse = webClient
                .delete()
                .uri(String.format("/files/local/%s", file.getName()))
                .exchange()
                .block(); // TODO: prevent blocking mono
        if (clientResponse …
Run Code Online (Sandbox Code Playgroud)

java reactor reactive-programming spring-webflux

5
推荐指数
1
解决办法
3609
查看次数