在响应中,我在将通量作为 ResponseEntity 返回时得到 scanAvailable=true

In the Response, i am getting scanAvailable=true when returning flux as ResponseEntity

当用户尝试将两个剩余端点与 WebClient 一起使用时,我对获得正确响应感到有些困惑。我想在代码中将其用作异步和非阻塞

我正在从控制器返回 Flux。代码详情如下:

控制器 Class 方法如下所示:

@RequestMapping(method = RequestMethod.GET, path = "/v1/repos/{userName}", produces = "application/json")
    public ResponseEntity<Flux<GithubRepo>> getUserReposDetails(
            @PathVariable(name="userName",required = true) String userName) throws Exception{
        return new ResponseEntity<>(this.getGitRepos(userName), HttpStatus.OK);
    }

正在调用getGitRepos方法。方法详情如下:

private Flux<GithubRepo> getGitRepos(String userName) {
        return webClient.get().uri("/users/{userName}/repos",userName).
                exchangeToFlux(clientResponse -> clientResponse.bodyToFlux(GithubRepo.class)).map(github->{
 github.setBranch(webClient.get()
.uri("/repos/{userName}/{branchName}/branches",userName,github.getBranch())
.retrieve().bodyToFlux(Branch.class).collectList());
                    return github;
                });
    }

WebClient 是:

WebClient webClient =  WebClient.builder().baseUrl("https://api.github.com").
            defaultHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.github.v3+json").build();

GitHub 和 Branch Class 如下:

@Data
public class GithubRepo {
    private String name;
    private String ownerLogin;
    private Mono<List<Branch>> branch;
    
    @JsonProperty("owner")
    private void unpackNested(Map<String,String> commit) {
        this.ownerLogin = commit.get("login");
    }
}
@Data
public class Branch {
    
    private String name;
    private Boolean protected;
}

我收到的 JSON 响应为:

[
  {
    "name": "HelloWorld",
    "ownerLogin": "test",
    "branch": {
      "scanAvailable": true
    }
  },
  {
    "name": "rokehan",
    "ownerLogin": "test",
    "branch": {
      "scanAvailable": true
    }
  },
  {
    "name": "sNews",
    "ownerLogin": "test",
    "branch": {
      "scanAvailable": true
    }
  },
  {
    "name": "Test--01",
    "ownerLogin": "test",
    "branch": {
      "scanAvailable": true
    }
  }
]

我希望得到如下响应:

[
  {
    "name": "HelloWorld",
    "ownerLogin": "test",
    "branch": [
      {
        "name": "asd",
        "protected": false
      },
      {
        "name": "master",
        "protected": false
      }
    ]
  },
  {
    "name": "rokehan",
    "ownerLogin": "test",
    "branch": [
      {
        "name": "master",
        "protected": false
      }
    ]
  },
  {
    "name": "sNews",
    "ownerLogin": "test",
    "branch": []
  },
  {
    "name": "Test--01",
    "ownerLogin": "test",
    "branch": [
      {
        "name": "master",
        "protected": false
      }
    ]
  }
]

请帮我解决这个问题。

我没有坐在电脑旁,所以我无法检查编译器(我在手机上写这个)

但问题是您正在尝试序列化一个 Mono<List<T>>,这意味着您正在尝试发送可能尚未解决的问题。你需要return一个List<T>.

private Flux<MyResponse> getGitRepos(String userName) {
        return webClient.get()
                        .uri("/users/{userName}/repos", userName)
                        .exchangeToFlux(clientResponse -> clientResponse.bodyToFlux(GithubRepo.class))
                        .flatMap(github -> {
                            return webClient.get()
                                            .uri("/repos/{userName}/{branchName}/branches", userName, github.getBranch())
                                            .retrieve()
                                            .bodyToFlux(Branch.class)
                                            .collectList())
                                            .flatMap(branches -> {
                                                return Mono.just(new MyResponse(github, branches));
                                            });
                });
    }

我是徒手写的,但你应该明白了。有一个 class 反对 github api,另一个反对你的 api 这样你就可以自由地改变你的 api 如果 api 反对 github 变化。

谢谢,@Toerktumlare 帮助我。更新后的方法是:

private Flux<MyResponse> getGitRepos(String userName) {
        return webClient.get()
                                  .uri("/users/{userName}/repos", userName)
                                  .exchangeToFlux(clientResponse -> clientResponse.bodyToFlux(GithubRepo.class))
                                  .flatMap(github -> {
                                      return webClient.get()
                                            .uri("/repos/{userName}/{branchName}/branches", userName, github.getName())
                                            .retrieve()
                                            .bodyToFlux(Branch.class)
                                            .collectList()
                                            .flatMap(branches -> {
                                                return Mono.just(new MyResponse(github, branches));
                                            });
                });
    }