Skip to content

Commit

Permalink
DefaultWebClientBuilder defaults using classpath
Browse files Browse the repository at this point in the history
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 spring-projectsgh-23491
  • Loading branch information
rwinch authored and rstoyanchev committed Aug 21, 2019
1 parent 0a7fdb3 commit ca3a0b1
Showing 1 changed file with 24 additions and 1 deletion.
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

0 comments on commit ca3a0b1

Please sign in to comment.