spring-framework/framework-docs/modules/ROOT/pages/web/webflux-webclient/client-context.adoc

38 lines
1.0 KiB
Plaintext
Raw Normal View History

2023-04-19 23:26:16 +08:00
[[webflux-client-context]]
= Context
2023-04-19 23:26:17 +08:00
xref:web/webflux-webclient/client-attributes.adoc[Attributes] provide a convenient way to pass information to the filter
2023-04-19 23:26:16 +08:00
chain but they only influence the current request. If you want to pass information that
propagates to additional requests that are nested, for example, via `flatMap`, or executed after,
for example, via `concatMap`, then you'll need to use the Reactor `Context`.
2023-04-19 23:26:16 +08:00
The Reactor `Context` needs to be populated at the end of a reactive chain in order to
apply to all operations. For example:
2023-04-21 05:21:36 +08:00
[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim,quotes"]
2023-04-19 23:26:16 +08:00
----
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", ...));
----
2023-04-21 05:21:36 +08:00
======
2023-04-19 23:26:16 +08:00