|
此版本仍在开发中,尚未被视为稳定版。如需最新稳定版本,请使用 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", ...));