如何在WebClient中返回Flux.just的状态

时间:2019-11-15 00:07:15

标签: java spring-webflux

我有一个问题

如何在状态5xx上返回空助焊剂?

@Override
public Flux<Repository> getAllUserRepositories(User user) {
    return WebClient.create()
            .get()
            .uri(hostname + ":" + port + "/user" + "/repos" + "?access_token=" + ACCESS_TOKEN + "&type=private")
            .retrieve()
            .onStatus(HttpStatus::is5xxServerError, clientResponse -> Mono.error(new WebServerException("Problem with service", null)))
            .bodyToFlux(Repository.class)
            .filter(repository -> repository.getName().contains(user.getName()));
}

1 个答案:

答案 0 :(得分:1)

您可以如下使用onErrorResume

return WebClient.create()
    .get()
    .uri(hostname + ":" + port + "/user" + "/repos" + "?access_token=" + ACCESS_TOKEN + "&type=private")
    .retrieve()
    .onStatus(HttpStatus::is5xxServerError, clientResponse -> Mono.error(new WebServerException("Problem with service", null)))
    .bodyToFlux(Repository.class)
    .onErrorResume(WebServerException.class, (e) -> Flux.empty())
    .filter(repository -> repository.getName().contains(user.getName()));