簡體   English   中英

如何將 Spring WebClient 響應轉換為 ResponseEntity?

[英]How to convert Spring WebClient Response to ResponseEntity?

我可以使用 toEntity() 方法返回 ResponseEntity,如下所示:

@GetMapping("/uri")
public Mono<ResponseEntity<Data[]>> methodName() {
    return webClient
            .get()
            .uri("http://localhost:8088/externalService")
            .accept(MediaType.APPLICATION_JSON)
            .retrieve()
            .toEntity(Data[].class);
}

但我想在返回之前處理響應標頭。 上面的代碼將 WebClient 響應轉換為 ResponseEntity 並立即返回,但我想將其存儲在 ResponseEntity 變量中,對其進行處理,然后將 ResponseEntity 返回。

我提到了這個-> Spring WebClient 文檔

當我嘗試將其存儲在變量中時,出現此錯誤->“block()/blockFirst()/blockLast() 正在阻塞,線程 reactor-http-nio-3 不支持”

您可以簡單地使用 Reactor 的map運算符來修改標題:

return webClient
        .get()
        .uri("http://localhost:8088/externalService")
        .accept(MediaType.APPLICATION_JSON)
        .retrieve()
        .toEntity(Data[].class)
        .map(response ->  response.getHeaders().add("header", "header-value");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM