Skip to content

Commit 49c463b

Browse files
committed
Polish RestClient request factories
This commit changes the default request factory from the SimpleClientHttpRequestFactory to the JdkClientHttpRequestFactory if available. It also adds detection logic for OkHttp and Jetty.
1 parent 094eb3e commit 49c463b

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

framework-docs/modules/ROOT/pages/integration/rest-clients.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The Spring Framework provides the following choices for making calls to REST end
1313
== `RestClient`
1414

1515
Reference documentation is forthcoming.
16-
For now, please refer to the https://docs.spring.io/spring-framework/docs/6.1.0-M1/javadoc-api/org/springframework/web/client/RestClient.html[API documentation].
16+
For now, please refer to the https://docs.spring.io/spring-framework/docs/6.1.0-M2/javadoc-api/org/springframework/web/client/RestClient.html[API documentation].
1717

1818

1919
[[rest-webclient]]

spring-web/src/main/java/org/springframework/web/client/DefaultRestClientBuilder.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import org.springframework.http.client.ClientHttpRequestInitializer;
3131
import org.springframework.http.client.ClientHttpRequestInterceptor;
3232
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
33+
import org.springframework.http.client.JdkClientHttpRequestFactory;
34+
import org.springframework.http.client.JettyClientHttpRequestFactory;
35+
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
3336
import org.springframework.http.client.SimpleClientHttpRequestFactory;
3437
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
3538
import org.springframework.http.converter.HttpMessageConverter;
@@ -57,8 +60,18 @@
5760
*/
5861
final class DefaultRestClientBuilder implements RestClient.Builder {
5962

63+
// request factories
64+
6065
private static final boolean httpComponentsClientPresent;
6166

67+
private static final boolean okHttpClientPresent;
68+
69+
private static final boolean jettyClientPresent;
70+
71+
private static final boolean jdkClientPresent;
72+
73+
// message factories
74+
6275
private static final boolean jackson2Present;
6376

6477
private static final boolean gsonPresent;
@@ -74,7 +87,12 @@ final class DefaultRestClientBuilder implements RestClient.Builder {
7487

7588
static {
7689
ClassLoader loader = DefaultRestClientBuilder.class.getClassLoader();
90+
7791
httpComponentsClientPresent = ClassUtils.isPresent("org.apache.hc.client5.http.classic.HttpClient", loader);
92+
okHttpClientPresent = ClassUtils.isPresent("okhttp3.OkHttpClient", loader);
93+
jettyClientPresent = ClassUtils.isPresent("org.eclipse.jetty.client.HttpClient", loader);
94+
jdkClientPresent = ClassUtils.isPresent("java.net.http.HttpClient", loader);
95+
7896
jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", loader) &&
7997
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", loader);
8098
gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", loader);
@@ -345,6 +363,16 @@ private ClientHttpRequestFactory initRequestFactory() {
345363
else if (httpComponentsClientPresent) {
346364
return new HttpComponentsClientHttpRequestFactory();
347365
}
366+
else if (okHttpClientPresent) {
367+
return new OkHttp3ClientHttpRequestFactory();
368+
}
369+
else if (jettyClientPresent) {
370+
return new JettyClientHttpRequestFactory();
371+
}
372+
else if (jdkClientPresent) {
373+
// java.net.http module might not be loaded, so we can't default to the JDK HttpClient
374+
return new JdkClientHttpRequestFactory();
375+
}
348376
else {
349377
return new SimpleClientHttpRequestFactory();
350378
}

spring-web/src/main/java/org/springframework/web/client/RestClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,15 @@ Builder defaultStatusHandler(Predicate<HttpStatusCode> statusPredicate,
346346
* Configure the {@link ClientHttpRequestFactory} to use. This is useful
347347
* for plugging in and/or customizing options of the underlying HTTP
348348
* client library (e.g. SSL).
349+
* <p>If no request factory is specified, {@code RestClient} uses
350+
* {@linkplain org.springframework.http.client.HttpComponentsClientHttpRequestFactory Apache Http Client},
351+
* {@linkplain org.springframework.http.client.OkHttp3ClientHttpRequestFactory OkHttp 3}, or
352+
* {@linkplain org.springframework.http.client.JettyClientHttpRequestFactory Jetty Http Client}
353+
* if available on the classpath, and defaults to the
354+
* {@linkplain org.springframework.http.client.JdkClientHttpRequestFactory JDK HttpClient}
355+
* if the {@code java.net.http} module is loaded, or to a
356+
* {@linkplain org.springframework.http.client.SimpleClientHttpRequestFactory simple default}
357+
* otherwise.
349358
* @param requestFactory the request factory to use
350359
* @return this builder
351360
*/

0 commit comments

Comments
 (0)