Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;

import io.micrometer.observation.ObservationRegistry;

Expand Down Expand Up @@ -387,6 +388,12 @@ public RestClient.Builder requestFactory(ClientHttpRequestFactory requestFactory
return this;
}

@Override
public RestClient.Builder requestFactory(Supplier<ClientHttpRequestFactory> requestFactorySupplier) {
this.requestFactory = requestFactorySupplier.get();
return this;
}

@Override
public RestClient.Builder messageConverters(Consumer<List<HttpMessageConverter<?>>> configurer) {
configurer.accept(initMessageConverters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import io.micrometer.observation.ObservationRegistry;

Expand Down Expand Up @@ -417,6 +418,23 @@ Builder defaultStatusHandler(Predicate<HttpStatusCode> statusPredicate,
*/
Builder requestFactory(ClientHttpRequestFactory requestFactory);

/**
* Configure the {@link ClientHttpRequestFactory} to use. This is useful
* for plugging in and/or customizing options of the underlying HTTP
* client library (for example, SSL).
* <p>If no request factory is specified, {@code RestClient} uses
* {@linkplain org.springframework.http.client.HttpComponentsClientHttpRequestFactory Apache Http Client},
* {@linkplain org.springframework.http.client.JettyClientHttpRequestFactory Jetty Http Client}
* if available on the classpath, and defaults to the
* {@linkplain org.springframework.http.client.JdkClientHttpRequestFactory JDK HttpClient}
* if the {@code java.net.http} module is loaded, or to a
* {@linkplain org.springframework.http.client.SimpleClientHttpRequestFactory simple default}
* otherwise.
* @param requestFactorySupplier the supplier for the request factory to use
* @return this builder
*/
Builder requestFactory(Supplier<ClientHttpRequestFactory> requestFactorySupplier);

/**
* Configure the message converters for the {@code RestClient} to use.
* @param configurer the configurer to apply on the list of default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.springframework.http.client.ClientHttpRequestInitializer;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.http.client.JettyClientHttpRequestFactory;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.http.converter.HttpMessageConverter;
Expand Down Expand Up @@ -283,4 +284,13 @@ private static Object fieldValue(String name, RestClient instance) {
return null;
}
}

@Test
void requestFactorySupplier() {
RestClient.Builder builder = RestClient.builder(new RestTemplate());
builder.requestFactory(() -> new JdkClientHttpRequestFactory());
DefaultRestClientBuilder defaultBuilder = (DefaultRestClientBuilder) builder;
assertThat(fieldValue("requestFactory", defaultBuilder)).isInstanceOf(JdkClientHttpRequestFactory.class);
}

}