Skip to content

Commit 26a2d38

Browse files
committed
Expose ClientCodecConfigurer in WebClient.Builder
Using Consumer<ClientCodecConfigurer> instead of Consumer<ExchangeStrategies> eliminates one level of nesting that is also unnecessary since codecs are the only strategy at present. Backport of dd9b628 Closes gh-24201
1 parent 802d083 commit 26a2d38

File tree

5 files changed

+60
-24
lines changed

5 files changed

+60
-24
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.http.HttpHeaders;
2424
import org.springframework.http.client.reactive.ClientHttpConnector;
2525
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
26+
import org.springframework.http.codec.ClientCodecConfigurer;
2627
import org.springframework.lang.Nullable;
2728
import org.springframework.util.Assert;
2829
import org.springframework.util.MultiValueMap;
@@ -136,12 +137,19 @@ public WebTestClient.Builder filters(Consumer<List<ExchangeFilterFunction>> filt
136137
return this;
137138
}
138139

140+
@Override
141+
public WebTestClient.Builder codecs(Consumer<ClientCodecConfigurer> configurer) {
142+
this.webClientBuilder.codecs(configurer);
143+
return this;
144+
}
145+
139146
@Override
140147
public WebTestClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
141148
this.webClientBuilder.exchangeStrategies(strategies);
142149
return this;
143150
}
144151

152+
@SuppressWarnings("deprecation")
145153
@Override
146154
public WebTestClient.Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer) {
147155
this.webClientBuilder.exchangeStrategies(configurer);

spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.http.MediaType;
3737
import org.springframework.http.client.reactive.ClientHttpConnector;
3838
import org.springframework.http.client.reactive.ClientHttpRequest;
39+
import org.springframework.http.codec.ClientCodecConfigurer;
3940
import org.springframework.http.codec.ServerCodecConfigurer;
4041
import org.springframework.lang.Nullable;
4142
import org.springframework.util.MultiValueMap;
@@ -435,14 +436,22 @@ interface Builder {
435436
*/
436437
Builder filters(Consumer<List<ExchangeFilterFunction>> filtersConsumer);
437438

439+
/**
440+
* Configure the codecs for the {@code WebClient} in the
441+
* {@link #exchangeStrategies(ExchangeStrategies) underlying}
442+
* {@code ExchangeStrategies}.
443+
* @param configurer the configurer to apply
444+
* @since 5.1.13
445+
*/
446+
Builder codecs(Consumer<ClientCodecConfigurer> configurer);
447+
438448
/**
439449
* Configure the {@link ExchangeStrategies} to use.
440-
* <p>Note that in a scenario where the builder is configured by
441-
* multiple parties, it is preferable to use
442-
* {@link #exchangeStrategies(Consumer)} in order to customize the same
443-
* {@code ExchangeStrategies}. This method here sets the strategies that
444-
* everyone else then can customize.
445-
* <p>By default this is {@link ExchangeStrategies#withDefaults()}.
450+
* <p>For most cases, prefer using {@link #codecs(Consumer)} which allows
451+
* customizing the codecs in the {@code ExchangeStrategies} rather than
452+
* replace them. That ensures multiple parties can contribute to codecs
453+
* configuration.
454+
* <p>By default this is set to {@link ExchangeStrategies#withDefaults()}.
446455
* @param strategies the strategies to use
447456
*/
448457
Builder exchangeStrategies(ExchangeStrategies strategies);
@@ -452,8 +461,9 @@ interface Builder {
452461
* {@link #exchangeStrategies(ExchangeStrategies)}. This method is
453462
* designed for use in scenarios where multiple parties wish to update
454463
* the {@code ExchangeStrategies}.
455-
* @since 5.1.12
464+
* @deprecated as of 5.1.13 in favor of {@link #codecs(Consumer)}
456465
*/
466+
@Deprecated
457467
Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer);
458468

459469
/**

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.http.client.reactive.ClientHttpConnector;
2828
import org.springframework.http.client.reactive.JettyClientHttpConnector;
2929
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
30+
import org.springframework.http.codec.ClientCodecConfigurer;
3031
import org.springframework.lang.Nullable;
3132
import org.springframework.util.Assert;
3233
import org.springframework.util.ClassUtils;
@@ -206,12 +207,22 @@ public WebClient.Builder clientConnector(ClientHttpConnector connector) {
206207
return this;
207208
}
208209

210+
@Override
211+
public WebClient.Builder codecs(Consumer<ClientCodecConfigurer> configurer) {
212+
if (this.strategiesConfigurers == null) {
213+
this.strategiesConfigurers = new ArrayList<>(4);
214+
}
215+
this.strategiesConfigurers.add(builder -> builder.codecs(configurer));
216+
return this;
217+
}
218+
209219
@Override
210220
public WebClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
211221
this.strategies = strategies;
212222
return this;
213223
}
214224

225+
@SuppressWarnings("deprecation")
215226
@Override
216227
public WebClient.Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer) {
217228
if (this.strategiesConfigurers == null) {

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.http.MediaType;
3838
import org.springframework.http.client.reactive.ClientHttpConnector;
3939
import org.springframework.http.client.reactive.ClientHttpRequest;
40+
import org.springframework.http.codec.ClientCodecConfigurer;
4041
import org.springframework.util.MultiValueMap;
4142
import org.springframework.web.reactive.function.BodyInserter;
4243
import org.springframework.web.reactive.function.BodyInserters;
@@ -288,14 +289,22 @@ interface Builder {
288289
*/
289290
Builder clientConnector(ClientHttpConnector connector);
290291

292+
/**
293+
* Configure the codecs for the {@code WebClient} in the
294+
* {@link #exchangeStrategies(ExchangeStrategies) underlying}
295+
* {@code ExchangeStrategies}.
296+
* @param configurer the configurer to apply
297+
* @since 5.1.13
298+
*/
299+
Builder codecs(Consumer<ClientCodecConfigurer> configurer);
300+
291301
/**
292302
* Configure the {@link ExchangeStrategies} to use.
293-
* <p>Note that in a scenario where the builder is configured by
294-
* multiple parties, it is preferable to use
295-
* {@link #exchangeStrategies(Consumer)} in order to customize the same
296-
* {@code ExchangeStrategies}. This method here sets the strategies that
297-
* everyone else then can customize.
298-
* <p>By default this is {@link ExchangeStrategies#withDefaults()}.
303+
* <p>For most cases, prefer using {@link #codecs(Consumer)} which allows
304+
* customizing the codecs in the {@code ExchangeStrategies} rather than
305+
* replace them. That ensures multiple parties can contribute to codecs
306+
* configuration.
307+
* <p>By default this is set to {@link ExchangeStrategies#withDefaults()}.
299308
* @param strategies the strategies to use
300309
*/
301310
Builder exchangeStrategies(ExchangeStrategies strategies);
@@ -305,15 +314,17 @@ interface Builder {
305314
* {@link #exchangeStrategies(ExchangeStrategies)}. This method is
306315
* designed for use in scenarios where multiple parties wish to update
307316
* the {@code ExchangeStrategies}.
308-
* @since 5.1.12
317+
* @deprecated as of 5.1.13 in favor of {@link #codecs(Consumer)}
309318
*/
319+
@Deprecated
310320
Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer);
311321

312322
/**
313323
* Provide an {@link ExchangeFunction} pre-configured with
314324
* {@link ClientHttpConnector} and {@link ExchangeStrategies}.
315325
* <p>This is an alternative to, and effectively overrides
316-
* {@link #clientConnector}, and {@link #exchangeStrategies}.
326+
* {@link #clientConnector}, and
327+
* {@link #exchangeStrategies(ExchangeStrategies)}.
317328
* @param exchangeFunction the exchange function to use
318329
*/
319330
Builder exchangeFunction(ExchangeFunction exchangeFunction);

src/docs/asciidoc/web/webflux.adoc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -908,20 +908,16 @@ The following example shows how to do so for client-side requests:
908908
[source,java,indent=0]
909909
[subs="verbatim,quotes"]
910910
----
911-
Consumer<ClientCodecConfigurer> consumer = configurer -> {
912-
CustomDecoder customDecoder = new CustomDecoder();
913-
configurer.customCodecs().decoder(customDecoder);
914-
configurer.customCodecs().withDefaultCodecConfig(config ->
915-
customDecoder.maxInMemorySize(config.maxInMemorySize())
916-
);
917-
}
918-
919911
WebClient webClient = WebClient.builder()
920-
.exchangeStrategies(strategies -> strategies.codecs(consumer))
912+
.codecs(configurer -> {
913+
CustomDecoder decoder = new CustomDecoder();
914+
configurer.customCodecs().registerWithDefaultConfig(decoder);
915+
})
921916
.build();
922917
----
923918
====
924919

920+
925921
[[webflux-dispatcher-handler]]
926922
== `DispatcherHandler`
927923
[.small]#<<web.adoc#mvc-servlet, Same as in Spring MVC>>#

0 commit comments

Comments
 (0)