Description
Everything below is looking at the code from Spring Boot 3.3.2
Looking at RestClientAutoConfiguration
, the following code is used to create a RestClient.Builder:
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
RestClient.Builder restClientBuilder(RestClientBuilderConfigurer restClientBuilderConfigurer) {
RestClient.Builder builder = RestClient.builder()
.requestFactory(ClientHttpRequestFactories.get(ClientHttpRequestFactorySettings.DEFAULTS));
return restClientBuilderConfigurer.configure(builder);
}
Comparing what the ClientHttpRequestFactories
does compared to the default settings already in the RestClient
, they seem very similar. The only difference I can tell is the Spring Boot ClientHttpRequestFactories
looks for okhttp3.OkHttpClient
while the DefaultRestClientBuilder
looks for java.net.http
.
I'm trying to find a good way to use the defaults provided by Spring Boot while doing custom enhancements outside of the service class itself so I can just inject the specific builder to match the practices I've used with RestTemplate
, however with the additional code to initialize the builder, it seems either I'm missing something or this is something that shouldn't have been there in the first place and needs to be taken out. From what I can see, the autoconfig should be able to be something like the following but I'm also thinking maybe this was intentional for reasons that's not immediately apparent:
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
RestClient.Builder restClientBuilder(RestClientBuilderConfigurer restClientBuilderConfigurer) {
RestClient.Builder builder = RestClient.builder();
return restClientBuilderConfigurer.configure(builder);
}
Let me know your thoughts and if needed I can provide the approaches I had in mind and you can tell me if maybe my thought process was in the wrong place or not.