Skip to content

Detect ClientHttpConnector to use by checking classpath #23493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@

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;
Expand All @@ -41,6 +43,17 @@
* @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;
Expand Down Expand Up @@ -234,8 +247,18 @@ else if (this.connector != null) {
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() {
Expand Down