|
对于最新稳定版本,请使用 Spring Framework 7.0.6! |
上下文
属性(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", ...));