Skip to content

Commit 2208c67

Browse files
committed
Add 'Global HTTP Client Configuration' reference docs section
Update documentation with information on how to configure the HTTP client globally. Closes gh-42888
1 parent e1b5935 commit 2208c67

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/io/rest-client.adoc

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ To make an application-wide, additive customization to all `RestClient.Builder`
112112
Finally, you can fall back to the original API and use `RestClient.create()`.
113113
In that case, no auto-configuration or `RestClientCustomizer` is applied.
114114

115+
TIP: You can also change the xref:io/rest-client.adoc#io.rest-client.clienthttprequestfactory.configuration[global HTTP client configuration].
116+
115117

116118

117119
[[io.rest-client.restclient.ssl]]
@@ -175,6 +177,8 @@ include-code::MyRestTemplateBuilderConfiguration[]
175177
The most extreme (and rarely used) option is to create your own `RestTemplateBuilder` bean without using a configurer.
176178
In addition to replacing the auto-configured builder, this also prevents any `RestTemplateCustomizer` beans from being used.
177179

180+
TIP: You can also change the xref:io/rest-client.adoc#io.rest-client.clienthttprequestfactory.configuration[global HTTP client configuration].
181+
178182

179183

180184
[[io.rest-client.resttemplate.ssl]]
@@ -195,7 +199,44 @@ In order of preference, the following clients are supported:
195199
. Apache HttpClient
196200
. Jetty HttpClient
197201
. Reactor Netty HttpClient
198-
. OkHttp (deprecated)
199-
. Simple JDK client (`HttpURLConnection`)
202+
. JDK client (`java.net.http.HttpClient`)
203+
. Simple JDK client (`java.net.HttpURLConnection`)
204+
205+
If multiple clients are available on the classpath, and not global configuration is provided, the most preferred client will be used.
206+
207+
208+
209+
[[io.rest-client.clienthttprequestfactory.configuration]]
210+
=== Global HTTP Client Configuration
211+
212+
If the the auto-detected HTTP client does not meet your needs, you can use the configprop:spring.http.client.factory[] property to pick a specific factory.
213+
For example, if you have Apache HttpClient on your classpath, but you prefer Jetty's `HttpClient` you can add use the following:
214+
215+
[configprops,yaml]
216+
----
217+
spring:
218+
http:
219+
client:
220+
factory: jetty
221+
----
222+
223+
You can also set properties to change defaults that will be applied to all clients.
224+
For example, you may want to change timeouts and if redirects are followed:
225+
226+
[configprops,yaml]
227+
----
228+
spring:
229+
http:
230+
client:
231+
connect-timeout: 2s
232+
read-timeout: 1s
233+
redirects: dont-follow
234+
----
235+
236+
For more complex customizations, you can declare your own `ClientHttpRequestFactoryBuilder` bean which will cause auto-configuration to back off.
237+
This can be useful when you need to customize some of the internals of the underlying HTTP library.
238+
239+
For example, the following will use a JDK client configured with a specific `java.net.ProxySelector`:
240+
241+
include-code::MyClientHttpConfiguration[]
200242

201-
If multiple clients are available on the classpath, the most preferred client will be used.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.io.restclient.clienthttprequestfactory.configuration;
18+
19+
import java.net.ProxySelector;
20+
21+
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
25+
@Configuration(proxyBeanMethods = false)
26+
public class MyClientHttpConfiguration {
27+
28+
@Bean
29+
ClientHttpRequestFactoryBuilder<?> clientHttpRequestFactoryBuilder(ProxySelector proxySelector) {
30+
return ClientHttpRequestFactoryBuilder.jdk()
31+
.withHttpClientCustomizer((builder) -> builder.proxy(proxySelector));
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.springframework.boot.docs.io.restclient.clienthttprequestfactory.configuration
2+
3+
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder
4+
import org.springframework.context.annotation.Bean
5+
import org.springframework.context.annotation.Configuration
6+
import java.net.ProxySelector
7+
import java.net.http.HttpClient
8+
9+
@Configuration(proxyBeanMethods = false)
10+
class MyClientHttpConfiguration {
11+
12+
@Bean
13+
fun clientHttpRequestFactoryBuilder(proxySelector: ProxySelector): ClientHttpRequestFactoryBuilder<*>? {
14+
return ClientHttpRequestFactoryBuilder.jdk()
15+
.withHttpClientCustomizer { builder -> builder.proxy(proxySelector) }
16+
}
17+
18+
}

0 commit comments

Comments
 (0)