上下文
屬性(Attributes) 提供了一種便捷的方式將資訊傳遞給過濾器鏈,但它們僅影響當前請求。如果你想傳遞能傳播到巢狀的(例如透過 `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", ...));