我正在使用 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?在這里感到迷茫和無能為力。在這方面的任何指示將不勝感激。
uj5u.com熱心網友回復:
我認為在該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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313135.html
標籤:爪哇 春天 弹簧靴 弹簧-webflux 项目反应堆
