上下文
属性 提供了一种便捷的方式来向过滤器链传递信息,但它们仅影响当前请求。如果你想传递在嵌套的额外请求(例如,通过 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", ...));