Polishing
See gh-23961
This commit is contained in:
@@ -91,12 +91,6 @@ final class DefaultExchangeStrategiesBuilder implements ExchangeStrategies.Build
|
||||
return Collections.unmodifiableList(new ArrayList<>(list));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public Builder mutate() {
|
||||
return new DefaultExchangeStrategiesBuilder(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<HttpMessageReader<?>> messageReaders() {
|
||||
return this.readers;
|
||||
@@ -106,6 +100,11 @@ final class DefaultExchangeStrategiesBuilder implements ExchangeStrategies.Build
|
||||
public List<HttpMessageWriter<?>> messageWriters() {
|
||||
return this.writers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder mutate() {
|
||||
return new DefaultExchangeStrategiesBuilder(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -81,8 +81,9 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
private ClientHttpConnector connector;
|
||||
|
||||
@Nullable
|
||||
private ExchangeStrategies.Builder strategies;
|
||||
private ExchangeStrategies strategies;
|
||||
|
||||
@Nullable
|
||||
private List<Consumer<ExchangeStrategies.Builder>> strategiesConfigurers;
|
||||
|
||||
@Nullable
|
||||
@@ -208,13 +209,6 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
@Override
|
||||
@Deprecated
|
||||
public WebClient.Builder exchangeStrategies(ExchangeStrategies strategies) {
|
||||
Assert.notNull(strategies, "ExchangeStrategies must not be null");
|
||||
this.strategies = strategies.mutate();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClient.Builder exchangeStrategies(ExchangeStrategies.Builder strategies) {
|
||||
Assert.notNull(strategies, "ExchangeStrategies must not be null");
|
||||
this.strategies = strategies;
|
||||
return this;
|
||||
@@ -222,6 +216,9 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
|
||||
@Override
|
||||
public WebClient.Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer) {
|
||||
if (this.strategiesConfigurers == null) {
|
||||
this.strategiesConfigurers = new ArrayList<>(4);
|
||||
}
|
||||
this.strategiesConfigurers.add(configurer);
|
||||
return this;
|
||||
}
|
||||
@@ -274,11 +271,11 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
@SuppressWarnings("deprecation")
|
||||
private ExchangeStrategies initExchangeStrategies() {
|
||||
if (CollectionUtils.isEmpty(this.strategiesConfigurers)) {
|
||||
return this.strategies != null ? this.strategies.build() : ExchangeStrategies.withDefaults();
|
||||
return this.strategies != null ? this.strategies : ExchangeStrategies.withDefaults();
|
||||
}
|
||||
|
||||
ExchangeStrategies.Builder builder =
|
||||
this.strategies != null ? this.strategies : ExchangeStrategies.builder();
|
||||
this.strategies != null ? this.strategies.mutate() : ExchangeStrategies.builder();
|
||||
|
||||
this.strategiesConfigurers.forEach(configurer -> configurer.accept(builder));
|
||||
return builder.build();
|
||||
|
||||
@@ -51,12 +51,9 @@ public interface ExchangeStrategies {
|
||||
* Return a builder to create a new {@link ExchangeStrategies} instance
|
||||
* replicated from the current instance.
|
||||
* @since 5.1.12
|
||||
* @deprecated APIs should consume {@link ExchangeStrategies} as final or accept an
|
||||
* {@link ExchangeStrategies.Builder builder}.
|
||||
*/
|
||||
@Deprecated
|
||||
default Builder mutate() {
|
||||
throw new UnsupportedOperationException("This ExchangeStrategies implementation does not support mutation.");
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -291,31 +291,22 @@ public interface WebClient {
|
||||
Builder clientConnector(ClientHttpConnector connector);
|
||||
|
||||
/**
|
||||
* Provide the {@link ExchangeStrategies} to use.
|
||||
* <p>This is useful for changing the default settings, yet still allowing
|
||||
* further customizations via {@link #exchangeStrategies(Consumer)}.
|
||||
* If not set, defaults are obtained from {@link ExchangeStrategies#withDefaults()}.
|
||||
* 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()}.
|
||||
* @param strategies the strategies to use
|
||||
* @deprecated as of 5.1, in favor of {@link #exchangeStrategies(ExchangeStrategies.Builder)}
|
||||
*/
|
||||
@Deprecated
|
||||
Builder exchangeStrategies(ExchangeStrategies strategies);
|
||||
|
||||
/**
|
||||
* Provide the {@link ExchangeStrategies.Builder} to use.
|
||||
* <p>This is useful for changing the default settings, yet still allowing
|
||||
* further customizations via {@link #exchangeStrategies(Consumer)}.
|
||||
* If not set, defaults are obtained from {@link ExchangeStrategies#builder()}.
|
||||
* @param strategies the strategies to use
|
||||
* @since 5.1.12
|
||||
*/
|
||||
Builder exchangeStrategies(ExchangeStrategies.Builder strategies);
|
||||
|
||||
/**
|
||||
* Customize the {@link ExchangeStrategies}.
|
||||
* <p>Allows further customization on {@link ExchangeStrategies},
|
||||
* mutating them if they were {@link #exchangeStrategies(ExchangeStrategies) set},
|
||||
* or starting from {@link ExchangeStrategies#withDefaults() defaults}.
|
||||
* Customize the strategies configured via
|
||||
* {@link #exchangeStrategies(ExchangeStrategies)}. This method is
|
||||
* designed for use in scenarios where multiple parties wish to update
|
||||
* the {@code ExchangeStrategies}.
|
||||
* @since 5.1.12
|
||||
*/
|
||||
Builder exchangeStrategies(Consumer<ExchangeStrategies.Builder> configurer);
|
||||
|
||||
@@ -20,6 +20,8 @@ import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -36,6 +38,8 @@ import org.springframework.core.NamedThreadLocal;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.FormHttpMessageReader;
|
||||
import org.springframework.http.codec.FormHttpMessageWriter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
@@ -45,6 +45,7 @@ public class ExchangeStrategiesTests {
|
||||
ExchangeStrategies strategies = ExchangeStrategies.empty().build();
|
||||
assertThat(strategies.messageReaders().isEmpty()).isTrue();
|
||||
assertThat(strategies.messageWriters().isEmpty()).isTrue();
|
||||
|
||||
ExchangeStrategies mutated = strategies.mutate().codecs(codecs -> codecs.registerDefaults(true)).build();
|
||||
assertThat(mutated.messageReaders().isEmpty()).isFalse();
|
||||
assertThat(mutated.messageWriters().isEmpty()).isFalse();
|
||||
|
||||
Reference in New Issue
Block a user