跳到主要内容

上下文

ChatGPT-4o 中英对照 Context

属性 提供了一种方便的方法来将信息传递给过滤器链,但它们只影响当前请求。如果你想传递的信息需要传播到嵌套的额外请求中,例如通过 flatMap,或者在之后执行,例如通过 concatMap,那么你需要使用 Reactor 的 Context

Reactor Context 需要在响应式链的末尾填充,以便应用于所有操作。例如:

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