上下文
屬性提供了一種方便的方式來向過濾器鏈傳遞資訊,但它們隻影響當前請求。如果您想傳遞在巢狀的附加請求(例如,透過 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", ...));