情境
屬性提供了一種方便的方式將資訊傳遞到篩選器鏈,但它們僅影響當前請求。如果您想傳遞傳播到其他巢狀請求(例如,透過 flatMap
)或之後執行的資訊(例如,透過 concatMap
),則您需要使用 Reactor Context
。
Reactor Context
需要在反應式鏈的末端填充,以便應用於所有操作。例如
-
Java
WebClient client = WebClient.builder()
.filter((request, next) ->
Mono.deferContextual(contextView -> {
String value = contextView.get("foo");
// ...
}))
.build();
client.get().uri("https://example.org/")
.retrieve()
.bodyToMono(String.class)
.flatMap(body -> {
// perform nested request (context propagates automatically)...
})
.contextWrite(context -> context.put("foo", ...));