Introduce ClientHttpConnectorBuilder support

Add a new `ClientHttpConnectorBuilder` interface to support the
creation of `ClientHttpConnector` instances. The new code is similar
to the `ClientHttpRequestFactoryBuilder` interface that was added to
Spring Boot 3.4.

The `ClientHttpConnectorBuilder` is a functional interface with
additional static factory methods for the various supported
`ClientHttpConnector` types. Each type has it's own builder
which to support client specific customization.

The previous auto-configuration has been relocated to the
`org.springframework.boot.autoconfigure.http.client.reactive`
package and updated to make use of the builder.

Closes gh-43079
This commit is contained in:
Phillip Webb
2025-03-27 14:24:39 -07:00
parent 4034725e38
commit 983e7b637b
68 changed files with 3682 additions and 1182 deletions

View File

@@ -52,6 +52,45 @@ You can learn more about the {url-spring-framework-docs}/web/webflux-webclient/c
[[io.rest-client.webclient.configuration]]
=== Global HTTP Connector Configuration
If the auto-detected javadoc:org.springframework.http.client.reactive.ClientHttpConnector[] does not meet your needs, you can use the configprop:spring.http.reactiveclient.settings.connector[] property to pick a specific connector.
For example, if you have Reactor Netty on your classpath, but you prefer Jetty's javadoc:org.eclipse.jetty.client.HttpClient[] you can add the following:
[configprops,yaml]
----
spring:
http:
reactiveclient:
settings:
connector: jetty
----
You can also set properties to change defaults that will be applied to all reactive connectors.
For example, you may want to change timeouts and if redirects are followed:
[configprops,yaml]
----
spring:
http:
reactiveclient:
settings:
connect-timeout: 2s
read-timeout: 1s
redirects: dont-follow
----
For more complex customizations, you can use javadoc:org.springframework.boot.autoconfigure.http.client.reactive.ClientHttpConnectorBuilderCustomizer[] or declare your own javadoc:org.springframework.boot.http.client.reactive.ClientHttpConnectorBuilder[] bean which will cause auto-configuration to back off.
This can be useful when you need to customize some of the internals of the underlying HTTP library.
For example, the following will use a JDK client configured with a specific javadoc:java.net.ProxySelector[]:
include-code::MyConnectorHttpConfiguration[]
[[io.rest-client.webclient.customization]]
=== WebClient Customization

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docs.io.restclient.webclient.configuration;
import java.net.ProxySelector;
import org.springframework.boot.http.client.reactive.ClientHttpConnectorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class MyConnectorHttpConfiguration {
@Bean
ClientHttpConnectorBuilder<?> clientHttpConnectorBuilder(ProxySelector proxySelector) {
return ClientHttpConnectorBuilder.jdk().withHttpClientCustomizer((builder) -> builder.proxy(proxySelector));
}
}

View File

@@ -0,0 +1,17 @@
package org.springframework.boot.docs.io.restclient.clienthttprequestfactory.configuration
import org.springframework.boot.http.client.reactive.ClientHttpConnectorBuilder;
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.net.ProxySelector
import java.net.http.HttpClient
@Configuration(proxyBeanMethods = false)
class MyConnectorHttpConfiguration {
@Bean
fun clientHttpConnectorBuilder(proxySelector: ProxySelector): ClientHttpConnectorBuilder<*> {
return ClientHttpConnectorBuilder.jdk().withHttpClientCustomizer { builder -> builder.proxy(proxySelector) }
}
}