Spring Web Reactive | 2. WebClient | 2.7. Context
Attributes provide a convenient way to pass information to the filter chain, but they affect only the current request. To pass information that propagates to nested requests executed through flatMap or after concatMap, use the Reactor Context.
The Reactor Context must be written at the end of a reactive chain so that it applies to all operations. For example:
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", ...));