上下文
属性(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", ...));