Context
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", ...));