|
此版本仍在开发中,尚未稳定。如需最新的稳定版本,请使用 Spring Framework 7.0.6! |
上下文
属性 提供了一种便捷的方式来向过滤器链传递信息,但它们只影响当前请求。如果你想传递那些会传播到嵌套的额外请求中的信息,例如,通过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", ...));