Skip to content

Commit 23006d4

Browse files
committed
Deprecate context method in WebClient
See gh-25710
1 parent d8dafbc commit 23006d4

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ public RequestBodySpec attributes(Consumer<Map<String, Object>> attributesConsum
306306
}
307307

308308
@Override
309+
@SuppressWarnings("deprecation")
309310
public RequestBodySpec context(Function<Context, Context> contextModifier) {
310311
this.contextModifier = (this.contextModifier != null ?
311312
this.contextModifier.andThen(contextModifier) : contextModifier);

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,14 @@ interface RequestHeadersSpec<S extends RequestHeadersSpec<S>> {
474474
S attributes(Consumer<Map<String, Object>> attributesConsumer);
475475

476476
/**
477-
* Provide a function to populate the Reactor {@code Context}. In contrast
478-
* to {@link #attribute(String, Object) attributes} which apply only to
479-
* the current request, the Reactor {@code Context} transparently propagates
480-
* to the downstream processing chain which may include other nested or
481-
* successive calls over HTTP or via other reactive clients.
477+
* Provide a function to populate the Reactor {@code Context}.
482478
* @param contextModifier the function to modify the context with
479+
* @deprecated in 5.3.2 to be removed soon after; this method cannot
480+
* provide context to downstream (nested or subsequent) requests and is
481+
* of limited value.
483482
* @since 5.3.1
484483
*/
484+
@Deprecated
485485
S context(Function<Context, Context> contextModifier);
486486

487487
/**

src/docs/asciidoc/web/webflux-webclient.adoc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,11 @@ For example:
951951
.awaitBody<Unit>()
952952
----
953953

954+
Note that you can configure a `defaultRequest` callback globally at the
955+
`WebClient.Builder` level which lets you insert attributes into all requests,
956+
which could be used for example in a Spring MVC application to populate
957+
request attributes based on `ThreadLocal` data.
958+
954959

955960
[[webflux-client-context]]
956961
== Context
@@ -960,10 +965,8 @@ chain but they only influence the current request. If you want to pass informati
960965
propagates to additional requests that are nested, e.g. via `flatMap`, or executed after,
961966
e.g. via `concatMap`, then you'll need to use the Reactor `Context`.
962967

963-
`WebClient` exposes a method to populate the Reactor `Context` for a given request.
964-
This information is available to filters for the current request and it also propagates
965-
to subsequent requests or other reactive clients participating in the downstream
966-
processing chain. For example:
968+
The Reactor `Context` needs to be populated at the end of a reactive chain in order to
969+
apply to all operations. For example:
967970

968971
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
969972
.Java
@@ -977,18 +980,14 @@ processing chain. For example:
977980
.build();
978981
979982
client.get().uri("https://example.org/")
980-
.context(context -> context.put("foo", ...))
981983
.retrieve()
982984
.bodyToMono(String.class)
983985
.flatMap(body -> {
984986
// perform nested request (context propagates automatically)...
985-
});
987+
})
988+
.contextWrite(context -> context.put("foo", ...));
986989
----
987990

988-
Note that you can also specify how to populate the context through the `defaultRequest`
989-
method at the level of the `WebClient.Builder` and that applies to all requests.
990-
This could be used for to example to pass information from `ThreadLocal` storage onto
991-
a Reactor processing chain in a Spring MVC application.
992991

993992

994993
[[webflux-client-synchronous]]

0 commit comments

Comments
 (0)