Spring WebClient - 记录 is2xxSuccessful HTTPStatus

Spring WebClient - logging is2xxSuccessful HTTPStatus

我正在使用 Spring WebClient 下载文件,如下所示:

private void dnloadFileAPI(String theId, String destination) {
        log.info("Downloading file.. " + theId);
        Flux<DataBuffer> dataBuffer = webClient
                .get()
                .uri("/some/fancy/" + theId + "/api")
                .retrieve()
                .onStatus(HttpStatus::is2xxSuccessful, response -> Mono.just(new CustomException("Success")))
                .bodyToFlux(DataBuffer.class);
        DataBufferUtils.write(dataBuffer, Paths.get(destination), StandardOpenOption.CREATE).share().block();
    }

文件下载正常。我唯一挣扎的是,当响应为 200 时,我只想像这样记录一行:

log.info("{}", theId + " - File downloaded successfully")

我也试过了,但没有得到我要找的东西 -

简而言之,有没有什么方法可以在不单独写 CustomException 的情况下实现?在这里感到迷茫和无能为力。在这方面的任何指示将不胜感激。

我认为在 bodyToFlux(DataBuffer.class) 行之后添加以下内容将是您所需要的

.doOnComplete(() -> log.info("File downloaded successfully"))

像这样

private void dnloadFileAPI(String theId, String destination) {
    log.info("Downloading file.. " + theId);
    Flux<DataBuffer> dataBuffer = webClient
        .get()
        .uri("/some/fancy/" + theId + "/api")
        .retrieve()
        .bodyToFlux(DataBuffer.class)
        .doOnComplete(() -> log.info("File downloaded successfully"));
    DataBufferUtils.write(dataBuffer, Paths.get(destination), StandardOpenOption.CREATE).share().block();
}