Browse Source

DefaultWebClientBuilder defaults using classpath

Previously DefaultWebClientBuilder always defaulted the ClientHttpConnector
with ReactorClientHttpConnector. This worked fine if reactor was used.
However, it would break if the user was trying to leverage Jetty.

This commit defaults to use Reactory Netty HttpClient if it is present. If
it is not present it then Jetty's HttpClient  is used if present.

Closes gh-23491
pull/23506/head
Rob Winch 5 years ago committed by Rossen Stoyanchev
parent
commit
ca3a0b19d6
  1. 25
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClientBuilder.java

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

@ -25,9 +25,11 @@ import java.util.function.Consumer; @@ -25,9 +25,11 @@ import java.util.function.Consumer;
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.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@ -41,6 +43,17 @@ import org.springframework.web.util.UriBuilderFactory; @@ -41,6 +43,17 @@ import org.springframework.web.util.UriBuilderFactory;
* @since 5.0
*/
final class DefaultWebClientBuilder implements WebClient.Builder {
private static final boolean jettyHttpClientPresent;
private static final boolean reactorNettyHttpClientPresent;
static {
ClassLoader classLoader = DefaultWebClientBuilder.class.getClassLoader();
jettyHttpClientPresent = ClassUtils.isPresent(
"org.eclipse.jetty.client.HttpClient", classLoader);
reactorNettyHttpClientPresent = ClassUtils.isPresent(
"reactor.netty.http.client.HttpClient", classLoader);
}
@Nullable
private String baseUrl;
@ -234,8 +247,18 @@ final class DefaultWebClientBuilder implements WebClient.Builder { @@ -234,8 +247,18 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
return ExchangeFunctions.create(this.connector, this.exchangeStrategies);
}
else {
return ExchangeFunctions.create(new ReactorClientHttpConnector(), this.exchangeStrategies);
return ExchangeFunctions.create(createDefaultClientHttpConnector(), this.exchangeStrategies);
}
}
private ClientHttpConnector createDefaultClientHttpConnector() {
if (reactorNettyHttpClientPresent) {
return new ReactorClientHttpConnector();
}
else if (jettyHttpClientPresent) {
return new JettyClientHttpConnector();
}
throw new IllegalStateException("No suitable default ClientHttpConnector found");
}
private UriBuilderFactory initUriBuilderFactory() {

Loading…
Cancel
Save