Skip to content

Commit 914f83f

Browse files
committed
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. Closes gh-35620
1 parent e937d8e commit 914f83f

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

framework-docs/modules/ROOT/pages/integration/rest-clients.adoc

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The Spring Framework provides the following choices for making calls to REST end
55

66
* xref:integration/rest-clients.adoc#rest-restclient[`RestClient`] -- synchronous client with a fluent API
77
* 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`
99
* xref:integration/rest-clients.adoc#rest-http-service-client[HTTP Service Clients] -- annotated interface backed by generated proxy
1010

1111

@@ -479,7 +479,8 @@ See xref:web/webflux-webclient.adoc[WebClient] for more details.
479479
The `RestTemplate` provides a high-level API over HTTP client libraries in the form of a classic Spring Template class.
480480
It exposes the following groups of overloaded methods:
481481

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.
483484
For asynchronous and streaming scenarios, consider the reactive xref:web/webflux-webclient.adoc[WebClient].
484485

485486
[[rest-overview-of-resttemplate-methods-tbl]]
@@ -547,10 +548,26 @@ See the xref:integration/observability.adoc#http-client.resttemplate[RestTemplat
547548
Objects passed into and returned from `RestTemplate` methods are converted to and from HTTP messages
548549
with the help of an `HttpMessageConverter`, see <<rest-message-conversion>>.
549550

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+
551569

552570
The following table shows `RestClient` equivalents for `RestTemplate` methods.
553-
It can be used to migrate from the latter to the former.
554571

555572
.RestClient equivalents for RestTemplate methods
556573
[cols="1,1", options="header"]
@@ -854,6 +871,11 @@ It can be used to migrate from the latter to the former.
854871

855872
|===
856873

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"].
878+
857879

858880
[[rest-http-service-client]]
859881
== HTTP Service Clients

0 commit comments

Comments
 (0)