Skip to content

Commit f078e05

Browse files
committed
Update docs on WebClient filters
1 parent 941186a commit f078e05

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public interface WebClient {
116116

117117

118118
/**
119-
* Return a builder to mutate properties of this web client.
119+
* Return a builder for a new {@code WebClient} with properties replicated
120+
* from the current {@code WebClient} instance, but without affecting it.
120121
*/
121122
Builder mutate();
122123

@@ -136,6 +137,7 @@ static WebClient create() {
136137
* A variant of {@link #create()} that accepts a default base URL. For more
137138
* details see {@link Builder#baseUrl(String) Builder.baseUrl(String)}.
138139
* @param baseUrl the base URI for all requests
140+
* @see #builder()
139141
*/
140142
static WebClient create(String baseUrl) {
141143
return new DefaultWebClientBuilder().baseUrl(baseUrl).build();

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

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -322,44 +322,77 @@ build a new `WebClient`, based on, but without affecting the current instance:
322322

323323

324324
[[webflux-client-filter]]
325-
== Filters
325+
== Client Filters
326326

327-
`WebClient` supports interception style request filtering:
327+
You can register an `ExchangeFilterFunction` in the `WebClient.Builder` to intercept and
328+
possibly modify requests performed through the client:
328329

329330
[source,java,intent=0]
330331
[subs="verbatim,quotes"]
331332
----
332-
WebClient client = WebClient.builder()
333-
.filter((request, next) -> {
334-
ClientRequest filtered = ClientRequest.from(request)
335-
.header("foo", "bar")
336-
.build();
337-
return next.exchange(filtered);
338-
})
339-
.build();
333+
WebClient client = WebClient.builder()
334+
.filter((request, next) -> {
335+
336+
ClientRequest filtered = ClientRequest.from(request)
337+
.header("foo", "bar")
338+
.build();
339+
340+
return next.exchange(filtered);
341+
})
342+
.build();
340343
----
341344

342-
`ExchangeFilterFunctions` provides a filter for basic authentication:
345+
This can be used for cross-cutting concerns such as authentication. The example below uses
346+
a filter for basic authentication through a static factory method:
343347

344348
[source,java,intent=0]
345349
[subs="verbatim,quotes"]
346350
----
347351
348-
// static import of ExchangeFilterFunctions.basicAuthentication
352+
// static import of ExchangeFilterFunctions.basicAuthentication
349353
350-
WebClient client = WebClient.builder()
351-
.filter(basicAuthentication("user", "pwd"))
352-
.build();
354+
WebClient client = WebClient.builder()
355+
.filter(basicAuthentication("user", "password"))
356+
.build();
353357
----
354358

355-
You can also mutate an existing `WebClient` instance without affecting the original:
359+
Filters apply globally to every request. To change how a filter's behavior for a specific
360+
request, you can add request attributes to the `ClientRequest` that can then be accessed
361+
by all filters in the chain:
356362

357363
[source,java,intent=0]
358364
[subs="verbatim,quotes"]
359365
----
360-
WebClient filteredClient = client.mutate()
361-
.filter(basicAuthentication("user", "pwd")
362-
.build();
366+
WebClient client = WebClient.builder()
367+
.filter((request, next) -> {
368+
Optional<Object> usr = request.attribute("myAttribute");
369+
// ...
370+
})
371+
.build();
372+
373+
client.get().uri("http://example.org/")
374+
.attribute("myAttribute", "...")
375+
.retrieve()
376+
.bodyToMono(Void.class);
377+
378+
}
379+
----
380+
381+
You can also replicate an existing `WebClient`, and insert new filters or remove already
382+
registered filters. In the example below, a basic authentication filter is inserted at
383+
index 0:
384+
385+
[source,java,intent=0]
386+
[subs="verbatim,quotes"]
387+
----
388+
389+
// static import of ExchangeFilterFunctions.basicAuthentication
390+
391+
WebClient client = webClient.mutate()
392+
.filters(filterList -> {
393+
filterList.add(0, basicAuthentication("user", "password"));
394+
})
395+
.build();
363396
----
364397

365398

0 commit comments

Comments
 (0)