I'm using Spring Boot 3.0.4 with Java 17. The Spring WebClient
documentation says to use the injected WebClient.Builder
:
Spring Boot creates and pre-configures a
WebClient.Builder
for you. It is strongly advised to inject it in your components and use it to createWebClient
instances. Spring Boot is configuring that builder to share HTTP resources, reflect codecs setup in the same fashion as the server ones …, and more.
The documentation also says:
Spring Boot will auto-detect which ClientHttpConnector to use to drive WebClient, depending on the libraries available on the application classpath. For now, Reactor Netty, Jetty ReactiveStream client, Apache HttpClient, and the JDK’s HttpClient are supported.
This is a bit unclear to me. I had read in books and articles that Spring Boot will use Netty automatically for WebClient
. But does this mean that without further configuration, the latest Spring Boot will use the JDK HttpClient
? Note that I have included spring-boot-starter-web
and spring-boot-starter-webflux
in my project, but nothing specifically relating to Netty.
Furthermore the Spring Reactor documentation tells me that I can configure a connection timeout like this if I am using the Netty runtime:
import io.netty.channel.ChannelOption;
HttpClient httpClient = HttpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
But what is the timeout default already, if I don't add this code? And if I don't like the default and want to use this code, how do I override the default WebClient.Builder
(mentioned above) without building one from scratch (and possibly negating all the other benefits)?
So let me summarize my doubts, based upon all this slightly ambiguous documentation:
- If I only specify
spring-boot-starter-web
andspring-boot-starter-webflux
, is SpringWebClient
using Netty or JDKHttpClient
. (If it's using Netty by default, why does the documentation even mention JDKHttpClient
? How would I force JDKHttpClient
?) - What are the default HTTP connection timeouts with the preconfigured
WebClient.Builder
, and where is this documented (or how can I find this out in the source code)? - How can I override just the connection timeout for the preconfigured
WebClient.Builder
which is injected into the Spring context automatically?
WebClient.Builder
bean can be done … substitutingWebClient.builder()
with the injectedWebClient.Builder
bean." Right, that now gives me aWebClient
that I can inject everywhere. But what if I want to keep injecting aWebClient.Builder
everywhere, using all the defaults Spring Boot already configures forWebClient.Builder
, except now the injectedWebClient.Builder
has a timeout configured? For example, individual beans may want to generate their ownWebClient
instances with a default API URI, using the injectedWebClient.Builder
. – Lewallen