|
对于最新的稳定版本,请使用 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", ...));