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
Improve RestTemplate to RestClient migration guide
This commit improve the reference documentation in order to:
* highlight the deprecation status of `RestTemplate`
* improve the migration guide for `RestClient`, better explaining
possible behavior differences and a migration strategy.
Closesgh-35620
Copy file name to clipboardExpand all lines: framework-docs/modules/ROOT/pages/integration/rest-clients.adoc
+26-4Lines changed: 26 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ The Spring Framework provides the following choices for making calls to REST end
5
5
6
6
* xref:integration/rest-clients.adoc#rest-restclient[`RestClient`] -- synchronous client with a fluent API
7
7
* xref:integration/rest-clients.adoc#rest-webclient[`WebClient`] -- non-blocking, reactive client with fluent API
8
-
* xref:integration/rest-clients.adoc#rest-resttemplate[`RestTemplate`] -- synchronous client with template method API
8
+
* xref:integration/rest-clients.adoc#rest-resttemplate[`RestTemplate`] -- synchronous client with template method API, now deprecated in favor of `RestClient`
9
9
* xref:integration/rest-clients.adoc#rest-http-service-client[HTTP Service Clients] -- annotated interface backed by generated proxy
10
10
11
11
@@ -479,7 +479,8 @@ See xref:web/webflux-webclient.adoc[WebClient] for more details.
479
479
The `RestTemplate` provides a high-level API over HTTP client libraries in the form of a classic Spring Template class.
480
480
It exposes the following groups of overloaded methods:
481
481
482
-
NOTE: The xref:integration/rest-clients.adoc#rest-restclient[`RestClient`] offers a more modern API for synchronous HTTP access.
482
+
WARNING: As of Spring Framework 7.0, `RestTemplate` is deprecated in favor of `RestClient` and will be removed in a future version,
483
+
please use the xref:integration/rest-clients.adoc#migrating-to-restclient["Migrating to RestClient"] guide.
483
484
For asynchronous and streaming scenarios, consider the reactive xref:web/webflux-webclient.adoc[WebClient].
484
485
485
486
[[rest-overview-of-resttemplate-methods-tbl]]
@@ -547,10 +548,26 @@ See the xref:integration/observability.adoc#http-client.resttemplate[RestTemplat
547
548
Objects passed into and returned from `RestTemplate` methods are converted to and from HTTP messages
548
549
with the help of an `HttpMessageConverter`, see <<rest-message-conversion>>.
549
550
550
-
=== Migrating from `RestTemplate` to `RestClient`
551
+
[[migrating-to-restclient]]
552
+
=== Migrating to `RestClient`
553
+
554
+
Applications can adopt `RestClient` in a gradual fashion, first focusing on API usage and then on infrastructure setup.
555
+
You can consider the following steps:
556
+
557
+
1. Create one or more `RestClient` from existing `RestTemplate` instances, like: `RestClient restClient = RestClient.create(restTemplate)`.
558
+
Gradually replace `RestTemplate` usage in your application, component by component, by focusing first on issuing requests.
559
+
See the table below for API equivalents.
560
+
2. Once all client requests go through `RestClient` instances, you can now work on replicating your existing
561
+
`RestTemplate` instance creations by using `RestClient.Builder`. Because `RestTemplate` and `RestClient`
562
+
share the same infrastructure, you can reuse custom `ClientHttpRequestFactory` or `ClientHttpRequestInterceptor`
563
+
in your setup. See xref:integration/rest-clients.adoc#rest-restclient[the `RestClient` builder API].
564
+
565
+
If no other library is available on the classpath, `RestClient` will choose the `JdkClientHttpRequestFactory`
566
+
powered by the modern JDK `HttpClient`, whereas `RestTemplate` would pick the `SimpleClientHttpRequestFactory` that
567
+
uses `HttpURLConnection`. This can explain subtle behavior difference at runtime at the HTTP level.
568
+
551
569
552
570
The following table shows `RestClient` equivalents for `RestTemplate` methods.
553
-
It can be used to migrate from the latter to the former.
554
571
555
572
.RestClient equivalents for RestTemplate methods
556
573
[cols="1,1", options="header"]
@@ -854,6 +871,11 @@ It can be used to migrate from the latter to the former.
854
871
855
872
|===
856
873
874
+
`RestClient` and `RestTemplate` instances share the same behavior when it comes to throwing exceptions
875
+
(with the `RestClientException` type being at the top of the hierarchy).
876
+
When `RestTemplate` consistently throws `HttpClientErrorException` for "4xx" response statues,
877
+
`RestClient` allows for more flexibility with custom xref:integration/rest-clients.adoc#rest-http-service-client-exceptions["status handlers"].
0 commit comments