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. Closes gh-24124
This commit is contained in:
parent
11e321b8e7
commit
dd9b6287b4
|
|
@ -23,6 +23,7 @@ import java.util.function.Consumer;
|
|||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.http.codec.ClientCodecConfigurer;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
|
@ -136,12 +137,19 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebTestClient.Builder codecs(Consumer<ClientCodecConfigurer> configurer) {
|
||||
this.webClientBuilder.codecs(configurer);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebTestClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
|
||||
this.webClientBuilder.exchangeStrategies(strategies);
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public WebTestClient.Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer) {
|
||||
this.webClientBuilder.exchangeStrategies(configurer);
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ import org.springframework.http.HttpMethod;
|
|||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.http.client.reactive.ClientHttpRequest;
|
||||
import org.springframework.http.codec.ClientCodecConfigurer;
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
|
@ -442,14 +443,22 @@ public interface WebTestClient {
|
|||
*/
|
||||
Builder filters(Consumer<List<ExchangeFilterFunction>> filtersConsumer);
|
||||
|
||||
/**
|
||||
* Configure the codecs for the {@code WebClient} in the
|
||||
* {@link #exchangeStrategies(ExchangeStrategies) underlying}
|
||||
* {@code ExchangeStrategies}.
|
||||
* @param configurer the configurer to apply
|
||||
* @since 5.1.13
|
||||
*/
|
||||
Builder codecs(Consumer<ClientCodecConfigurer> configurer);
|
||||
|
||||
/**
|
||||
* Configure the {@link ExchangeStrategies} to use.
|
||||
* <p>Note that in a scenario where the builder is configured by
|
||||
* multiple parties, it is preferable to use
|
||||
* {@link #exchangeStrategies(Consumer)} in order to customize the same
|
||||
* {@code ExchangeStrategies}. This method here sets the strategies that
|
||||
* everyone else then can customize.
|
||||
* <p>By default this is {@link ExchangeStrategies#withDefaults()}.
|
||||
* <p>For most cases, prefer using {@link #codecs(Consumer)} which allows
|
||||
* customizing the codecs in the {@code ExchangeStrategies} rather than
|
||||
* replace them. That ensures multiple parties can contribute to codecs
|
||||
* configuration.
|
||||
* <p>By default this is set to {@link ExchangeStrategies#withDefaults()}.
|
||||
* @param strategies the strategies to use
|
||||
*/
|
||||
Builder exchangeStrategies(ExchangeStrategies strategies);
|
||||
|
|
@ -459,8 +468,9 @@ public interface WebTestClient {
|
|||
* {@link #exchangeStrategies(ExchangeStrategies)}. This method is
|
||||
* designed for use in scenarios where multiple parties wish to update
|
||||
* the {@code ExchangeStrategies}.
|
||||
* @since 5.1.12
|
||||
* @deprecated as of 5.1.13 in favor of {@link #codecs(Consumer)}
|
||||
*/
|
||||
@Deprecated
|
||||
Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer);
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import org.springframework.http.HttpHeaders;
|
|||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.http.client.reactive.JettyClientHttpConnector;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.http.codec.ClientCodecConfigurer;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
|
@ -206,12 +207,22 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClient.Builder codecs(Consumer<ClientCodecConfigurer> configurer) {
|
||||
if (this.strategiesConfigurers == null) {
|
||||
this.strategiesConfigurers = new ArrayList<>(4);
|
||||
}
|
||||
this.strategiesConfigurers.add(builder -> builder.codecs(configurer));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
|
||||
this.strategies = strategies;
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public WebClient.Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer) {
|
||||
if (this.strategiesConfigurers == null) {
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import org.springframework.http.MediaType;
|
|||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.http.client.reactive.ClientHttpRequest;
|
||||
import org.springframework.http.codec.ClientCodecConfigurer;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.reactive.function.BodyInserter;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
|
|
@ -290,14 +291,22 @@ public interface WebClient {
|
|||
*/
|
||||
Builder clientConnector(ClientHttpConnector connector);
|
||||
|
||||
/**
|
||||
* Configure the codecs for the {@code WebClient} in the
|
||||
* {@link #exchangeStrategies(ExchangeStrategies) underlying}
|
||||
* {@code ExchangeStrategies}.
|
||||
* @param configurer the configurer to apply
|
||||
* @since 5.1.13
|
||||
*/
|
||||
Builder codecs(Consumer<ClientCodecConfigurer> configurer);
|
||||
|
||||
/**
|
||||
* Configure the {@link ExchangeStrategies} to use.
|
||||
* <p>Note that in a scenario where the builder is configured by
|
||||
* multiple parties, it is preferable to use
|
||||
* {@link #exchangeStrategies(Consumer)} in order to customize the same
|
||||
* {@code ExchangeStrategies}. This method here sets the strategies that
|
||||
* everyone else then can customize.
|
||||
* <p>By default this is {@link ExchangeStrategies#withDefaults()}.
|
||||
* <p>For most cases, prefer using {@link #codecs(Consumer)} which allows
|
||||
* customizing the codecs in the {@code ExchangeStrategies} rather than
|
||||
* replace them. That ensures multiple parties can contribute to codecs
|
||||
* configuration.
|
||||
* <p>By default this is set to {@link ExchangeStrategies#withDefaults()}.
|
||||
* @param strategies the strategies to use
|
||||
*/
|
||||
Builder exchangeStrategies(ExchangeStrategies strategies);
|
||||
|
|
@ -307,15 +316,17 @@ public interface WebClient {
|
|||
* {@link #exchangeStrategies(ExchangeStrategies)}. This method is
|
||||
* designed for use in scenarios where multiple parties wish to update
|
||||
* the {@code ExchangeStrategies}.
|
||||
* @since 5.1.12
|
||||
* @deprecated as of 5.1.13 in favor of {@link #codecs(Consumer)}
|
||||
*/
|
||||
@Deprecated
|
||||
Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer);
|
||||
|
||||
/**
|
||||
* Provide an {@link ExchangeFunction} pre-configured with
|
||||
* {@link ClientHttpConnector} and {@link ExchangeStrategies}.
|
||||
* <p>This is an alternative to, and effectively overrides
|
||||
* {@link #clientConnector}, and {@link #exchangeStrategies}.
|
||||
* {@link #clientConnector}, and
|
||||
* {@link #exchangeStrategies(ExchangeStrategies)}.
|
||||
* @param exchangeFunction the exchange function to use
|
||||
*/
|
||||
Builder exchangeFunction(ExchangeFunction exchangeFunction);
|
||||
|
|
|
|||
|
|
@ -979,31 +979,21 @@ The following example shows how to do so for client-side requests:
|
|||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||
.Java
|
||||
----
|
||||
Consumer<ClientCodecConfigurer> consumer = configurer -> {
|
||||
CustomDecoder customDecoder = new CustomDecoder();
|
||||
configurer.customCodecs().decoder(customDecoder);
|
||||
configurer.customCodecs().withDefaultCodecConfig(config ->
|
||||
customDecoder.maxInMemorySize(config.maxInMemorySize())
|
||||
);
|
||||
}
|
||||
|
||||
WebClient webClient = WebClient.builder()
|
||||
.exchangeStrategies(strategies -> strategies.codecs(consumer))
|
||||
.codecs(configurer -> {
|
||||
CustomDecoder decoder = new CustomDecoder();
|
||||
configurer.customCodecs().registerWithDefaultConfig(decoder);
|
||||
})
|
||||
.build();
|
||||
----
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||
.Kotlin
|
||||
----
|
||||
val consumer: (ClientCodecConfigurer) -> Unit = { configurer ->
|
||||
val customDecoder = CustomDecoder()
|
||||
configurer.customCodecs().decoder(customDecoder)
|
||||
configurer.customCodecs().withDefaultCodecConfig({ config ->
|
||||
customDecoder.maxInMemorySize(config.maxInMemorySize())
|
||||
})
|
||||
}
|
||||
|
||||
val webClient = WebClient.builder()
|
||||
.exchangeStrategies({ strategies -> strategies.codecs(consumer) })
|
||||
.codecs({ configurer ->
|
||||
val decoder = CustomDecoder()
|
||||
configurer.customCodecs().registerWithDefaultConfig(decoder)
|
||||
})
|
||||
.build()
|
||||
----
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue