我正在使用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")
我也试过了,但没有得到我正在寻找的-如何记录Spring WebClient响应
简而言之,是否有某种方法可以在不编写单独的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();
}
我们可以使用doOnSuccess方法
.doOnSuccess(response -> logger.info("File downloaded successfully"))
webflux version 5.3.22