You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simplify HTTP client configuration properties by sharing common
settings for both blocking and reactive clients.
The `ClientHttpRequestFactorySettings` and `ClientHttpConnectorSettings`
have been merged to a single `HttpClientSettings` class. Properties
to configure common settings are available under:
`spring.http.clients`
Blocking and reactive settings have been moved to
`spring.http.clients.blocking` and `spring.http.clients.reactive`. With
currently only the factory/connector being configurable.
HTTP Service Client properties have also been rationalized under a
`spring.http.serviceclient.<group-name>`. Support for properties that
apply to all service clients and all Rest/Web Clients have been removed.
Support for `ApiVerionInserter` beans has also been removed in favor of
configuring the service group or builders directly.
Closesgh-47398
Copy file name to clipboardExpand all lines: documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/io/rest-client.adoc
+57-80Lines changed: 57 additions & 80 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,29 +55,19 @@ You can learn more about the {url-spring-framework-docs}/web/webflux-webclient/c
55
55
[[io.rest-client.webclient.configuration]]
56
56
=== Global HTTP Connector Configuration
57
57
58
-
If the auto-detected javadoc:org.springframework.http.client.reactive.ClientHttpConnector[] does not meet your needs, you can use the configprop:spring.http.reactiveclient.connector[] property to pick a specific connector.
58
+
If the auto-detected javadoc:org.springframework.http.client.reactive.ClientHttpConnector[] does not meet your needs, you can use the configprop:spring.http.clients.reactive.connector[] property to pick a specific connector.
59
59
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:
60
60
61
61
[configprops,yaml]
62
62
----
63
63
spring:
64
64
http:
65
-
reactiveclient:
66
-
connector: jetty
65
+
clients:
66
+
reactive:
67
+
connector: jetty
67
68
----
68
69
69
-
You can also set properties to change defaults that will be applied to all reactive connectors.
70
-
For example, you may want to change timeouts and if redirects are followed:
71
-
72
-
[configprops,yaml]
73
-
----
74
-
spring:
75
-
http:
76
-
reactiveclient:
77
-
connect-timeout: 2s
78
-
read-timeout: 1s
79
-
redirects: dont-follow
80
-
----
70
+
TIP: You can also use xref:io/rest-client.adoc#io.rest-client.global-configuration[global configuration properties] which apply to all HTTP clients.
81
71
82
72
For more complex customizations, you can use javadoc:org.springframework.boot.http.client.autoconfigure.reactive.ClientHttpConnectorBuilderCustomizer[] or declare your own javadoc:org.springframework.boot.http.client.reactive.ClientHttpConnectorBuilder[] bean which will cause auto-configuration to back off.
83
73
This can be useful when you need to customize some of the internals of the underlying HTTP library.
If 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.
238
+
If the auto-detected HTTP client does not meet your needs, you can use the configprop:spring.http.clients.blocking.factory[] property to pick a specific factory.
250
239
For example, if you have Apache HttpClient on your classpath, but you prefer Jetty's javadoc:org.eclipse.jetty.client.HttpClient[] you can add the following:
251
240
252
241
[configprops,yaml]
253
242
----
254
243
spring:
255
244
http:
256
-
client:
257
-
factory: jetty
245
+
clients:
246
+
blocking:
247
+
factory: jetty
258
248
----
259
249
260
-
You can also set properties to change defaults that will be applied to all clients.
261
-
For example, you may want to change timeouts and if redirects are followed:
262
-
263
-
[configprops,yaml]
264
-
----
265
-
spring:
266
-
http:
267
-
client:
268
-
connect-timeout: 2s
269
-
read-timeout: 1s
270
-
redirects: dont-follow
271
-
----
250
+
TIP: You can also use xref:io/rest-client.adoc#io.rest-client.global-configuration[global configuration properties] which apply to all HTTP clients.
272
251
273
252
For more complex customizations, you can use javadoc:org.springframework.boot.http.client.autoconfigure.ClientHttpRequestFactoryBuilderCustomizer[] or declare your own javadoc:org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder[] bean which will cause auto-configuration to back off.
274
253
This can be useful when you need to customize some of the internals of the underlying HTTP library.
@@ -286,23 +265,6 @@ Both `WebClient` and `RestClient` support making versioned remote HTTP calls so
286
265
Commonly this involves sending an HTTP header, a query parameter or URL path segment that indicates the version of the API that should be used.
287
266
288
267
You can configure API versioning using methods on `WebClient.Builder` or `RestClient.Builder`.
289
-
You can also the `spring.http.reactiveclient.webclient.apiversion` or `spring.http.client.restclient.apiversion` properties if you want to apply the same configuration to all builders.
290
-
291
-
For example, the following adds an `X-Version` HTTP header to all calls from the `RestClient` and uses the version `1.0.0` unless overridden for specific requests:
292
-
293
-
[configprops,yaml]
294
-
----
295
-
spring:
296
-
http:
297
-
client:
298
-
restclient:
299
-
apiversion:
300
-
default: 1.0.0
301
-
insert:
302
-
header: X-Version
303
-
----
304
-
305
-
You can also define javadoc:org.springframework.web.client.ApiVersionInserter[] and javadoc:org.springframework.web.client.ApiVersionFormatter[] beans if you need more control of the way that version information should be inserted and formatted.
306
268
307
269
TIP: API versioning is also supported on the server-side.
308
270
See the xref:web/servlet.adoc#web.servlet.spring-mvc.api-versioning[Spring MVC] and xref:web/reactive.adoc#web.reactive.webflux.api-versioning[Spring WebFlux] sections for details.
@@ -363,32 +325,15 @@ We can then write:
363
325
364
326
include-code::MyApplication[]
365
327
366
-
And finally we can then use a `base-url` property to link the `echo` group to an actual URL.
367
-
368
-
For a `RestClient` backed HTTP Service client this would be:
328
+
And finally we can then use a `base-url` property to link the `echo` group to an actual URL:
369
329
370
330
[configprops,yaml]
371
331
----
372
332
spring:
373
333
http:
374
-
client:
375
-
service:
376
-
group:
377
-
echo:
378
-
base-url: "https://echo.zuplo.io"
379
-
----
380
-
381
-
For a `WebClient` backed HTTP Service client, we'd use the reactive variant:
382
-
383
-
[configprops,yaml]
384
-
----
385
-
spring:
386
-
http:
387
-
reactiveclient:
388
-
service:
389
-
group:
390
-
echo:
391
-
base-url: "https://echo.zuplo.io"
334
+
serviceclient:
335
+
echo:
336
+
base-url: "https://echo.zuplo.io"
392
337
----
393
338
394
339
TIP: HTTP Service clients will be associated with a group named "`default`" if you don't specify a group.
Configuration properties for HTTP Services can be specified under `spring.http.client.service` for `RestClient` backed clients and `spring.http.reactiveclient.service` for `WebClient` backed clients.
356
+
Configuration properties for HTTP Services can be specified under `spring.http.serviceclient.<group-name>`:
412
357
413
358
You can use properties to configure aspects such as:
414
359
@@ -419,26 +364,30 @@ You can use properties to configure aspects such as:
419
364
* Connection and read timeouts.
420
365
* SSL bundles to use.
421
366
422
-
Properties are hierarchical and can be specified per-group, or for all HTTP Service clients.
423
-
Some properties can also be specified as xref:/io/rest-client.adoc#io.rest-client.clienthttprequestfactory.configuration[global client configuration], so that they are considered for both HTTP Service clients and direct use of `RestClient` or `WebClient`.
367
+
TIP: You can also use xref:io/rest-client.adoc#io.rest-client.global-configuration[global configuration properties] which apply to all HTTP clients.
424
368
425
369
For example, the properties below will:
426
370
427
-
* Configure all HTTP Service client and `RestClient` beans to use a one second connect timeout (unless otherwise overridden).
428
-
* Configure all HTTP Service clients to use a two second read timeout (unless otherwise overridden).
429
-
* Configure HTTP Service clients in the "`echo`" group to use a specific base URL.
371
+
* Configure all HTTP clients to use a one second connect timeout (unless otherwise overridden).
372
+
* Configure HTTP Service clients in the "`echo`" group to:
373
+
** Use a specific base URL.
374
+
** Have a two second read timeout.
375
+
** Insert API version information using an `X-Version` header.
430
376
431
377
[configprops,yaml]
432
378
----
433
379
spring:
434
380
http:
435
-
client:
381
+
clients:
436
382
connect-timeout: 1s
437
-
service:
383
+
serviceclient:
384
+
echo:
385
+
base-url: "https://echo.zuplo.io"
438
386
read-timeout: 2s;
439
-
group:
440
-
echo:
441
-
base-url: "https://echo.zuplo.io"
387
+
apiversion:
388
+
default: 1.0.0
389
+
insert:
390
+
header: X-Version
442
391
----
443
392
444
393
@@ -466,3 +415,31 @@ You can javadoc:org.springframework.context.annotation.Import[format=annotation]
466
415
For more details, see {url-spring-framework-docs}/integration/rest-clients.html#rest-http-service-client-group-config[Spring Framework reference documentation].
467
416
468
417
Regardless of which method you use to register HTTP Service clients, Spring Boot's support remains the same.
418
+
419
+
420
+
421
+
[[io.rest-client.global-configuration]]
422
+
== Applying Global Configuration to All HTTP Clients
423
+
424
+
Regardless of the underlying technology being used, all HTTP clients have common settings that can be configured.
425
+
426
+
These include:
427
+
428
+
* Connection Timeouts.
429
+
* Read Timeouts.
430
+
* How HTTP redirects should be handled.
431
+
* Which SSL bundle should be used when connecting.
432
+
433
+
These common settings are represented by the javadoc:org.springframework.boot.http.client.HttpClientSettings[] class which can be passed into the `build(...)` methods of javadoc:org.springframework.boot.http.client.reactive.ClientHttpConnectorBuilder[] and javadoc:org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder[].
434
+
435
+
If you want to apply the same configuration to all auto-configured clients, you can use `spring.http.clients` properties to do so:
Copy file name to clipboardExpand all lines: documentation/spring-boot-docs/src/main/java/org/springframework/boot/docs/io/restclient/restclient/ssl/settings/MyService.java
Copy file name to clipboardExpand all lines: documentation/spring-boot-docs/src/main/java/org/springframework/boot/docs/io/webservices/template/MyWebServiceTemplateConfiguration.java
Copy file name to clipboardExpand all lines: documentation/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/io/restclient/restclient/ssl/settings/MyService.kt
Copy file name to clipboardExpand all lines: documentation/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/io/webservices/template/MyWebServiceTemplateConfiguration.kt
Copy file name to clipboardExpand all lines: module/spring-boot-http-client/src/main/java/org/springframework/boot/http/client/AbstractClientHttpRequestFactoryBuilder.java
Copy file name to clipboardExpand all lines: module/spring-boot-http-client/src/main/java/org/springframework/boot/http/client/ClientHttpRequestFactoryBuilder.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ default T build() {
61
61
* @param settings the settings to apply or {@code null}
62
62
* @return a fully configured {@link ClientHttpRequestFactory}.
0 commit comments