Skip to content

Commit 91d17cf

Browse files
committed
Add notes on future deprecation of the RestTemplate
Issue: SPR-16993
1 parent fcbf06f commit 91d17cf

File tree

5 files changed

+45
-80
lines changed

5 files changed

+45
-80
lines changed

spring-web/src/main/java/org/springframework/web/client/RestOperations.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
* @author Juergen Hoeller
3838
* @since 3.0
3939
* @see RestTemplate
40-
* @see AsyncRestOperations
4140
*/
4241
public interface RestOperations {
4342

spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -64,55 +64,20 @@
6464
import org.springframework.web.util.UriTemplateHandler;
6565

6666
/**
67-
* <strong>Spring's central class for synchronous client-side HTTP access.</strong>
68-
* It simplifies communication with HTTP servers, and enforces RESTful principles.
69-
* It handles HTTP connections, leaving application code to provide URLs
70-
* (with possible template variables) and extract results.
67+
* Synchronous client to perform HTTP requests, exposing a simple, template
68+
* method API over underlying HTTP client libraries such as the JDK
69+
* {@code HttpURLConnection}, Apache HttpComponents, and others.
7170
*
72-
* <p><strong>Note:</strong> by default the RestTemplate relies on standard JDK
73-
* facilities to establish HTTP connections. You can switch to use a different
74-
* HTTP library such as Apache HttpComponents, Netty, and OkHttp through the
75-
* {@link #setRequestFactory} property.
71+
* <p>The RestTemplate offers templates for common scenarios by HTTP method, in
72+
* addition to the generalized {@code exchange} and {@code execute} methods that
73+
* support of less frequent cases.
7674
*
77-
* <p>The main entry points of this template are the methods named after the six main HTTP methods:
78-
* <table>
79-
* <tr><th>HTTP method</th><th>RestTemplate methods</th></tr>
80-
* <tr><td>DELETE</td><td>{@link #delete}</td></tr>
81-
* <tr><td>GET</td><td>{@link #getForObject}</td></tr>
82-
* <tr><td></td><td>{@link #getForEntity}</td></tr>
83-
* <tr><td>HEAD</td><td>{@link #headForHeaders}</td></tr>
84-
* <tr><td>OPTIONS</td><td>{@link #optionsForAllow}</td></tr>
85-
* <tr><td>POST</td><td>{@link #postForLocation}</td></tr>
86-
* <tr><td></td><td>{@link #postForObject}</td></tr>
87-
* <tr><td>PUT</td><td>{@link #put}</td></tr>
88-
* <tr><td>any</td><td>{@link #exchange}</td></tr>
89-
* <tr><td></td><td>{@link #execute}</td></tr> </table>
90-
*
91-
* <p>In addition the {@code exchange} and {@code execute} methods are generalized versions of
92-
* the above methods and can be used to support additional, less frequent combinations (e.g.
93-
* HTTP PATCH, HTTP PUT with response body, etc.). Note however that the underlying HTTP
94-
* library used must also support the desired combination.
95-
*
96-
* <p><strong>Note:</strong> For URI templates it is assumed encoding is necessary, e.g.
97-
* {@code restTemplate.getForObject("http://example.com/hotel list")} becomes
98-
* {@code "http://example.com/hotel%20list"}. This also means if the URI template
99-
* or URI variables are already encoded, double encoding will occur, e.g.
100-
* {@code http://example.com/hotel%20list} becomes
101-
* {@code http://example.com/hotel%2520list}). To avoid that use a {@code URI} method
102-
* variant to provide (or re-use) a previously encoded URI. To prepare such an URI
103-
* with full control over encoding, consider using
104-
* {@link org.springframework.web.util.UriComponentsBuilder}.
105-
*
106-
* <p>Internally the template uses {@link HttpMessageConverter} instances to
107-
* convert HTTP messages to and from POJOs. Converters for the main mime types
108-
* are registered by default but you can also register additional converters
109-
* via {@link #setMessageConverters}.
110-
*
111-
* <p>This template uses a
112-
* {@link org.springframework.http.client.SimpleClientHttpRequestFactory} and a
113-
* {@link DefaultResponseErrorHandler} as default strategies for creating HTTP
114-
* connections or handling HTTP errors, respectively. These defaults can be overridden
115-
* through {@link #setRequestFactory} and {@link #setErrorHandler} respectively.
75+
* <p><strong>NOTE:</strong> As of 5.0, the non-blocking, reactive
76+
* {@link org.springframework.web.reactive.client.WebClient WebClient} offers a
77+
* modern alternative to the {@code RestTemplate} with efficient support for
78+
* both sync and async, as well as streaming scenarios. The {@code RestTemplate}
79+
* will be deprecated in a future version and will not have major new features
80+
* gong forward.
11681
*
11782
* @author Arjen Poutsma
11883
* @author Brian Clozel
@@ -123,7 +88,6 @@
12388
* @see RequestCallback
12489
* @see ResponseExtractor
12590
* @see ResponseErrorHandler
126-
* @see AsyncRestTemplate
12791
*/
12892
public class RestTemplate extends InterceptingHttpAccessor implements RestOperations {
12993

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,18 @@
4343
import org.springframework.web.util.UriBuilderFactory;
4444

4545
/**
46-
* A non-blocking, reactive client for performing HTTP requests with Reactive
47-
* Streams back pressure. Provides a higher level, common API over HTTP client
48-
* libraries. Reactor Netty is used by default but other clients may be plugged
49-
* in with a {@link ClientHttpConnector}.
46+
* Non-blocking, reactive client to perform HTTP requests, exposing a fluent,
47+
* reactive API over underlying HTTP client libraries such as Reactor Netty.
5048
*
51-
* <p>Use one of the static factory methods {@link #create()} or
52-
* {@link #create(String)} or obtain a {@link WebClient#builder()} to create an
53-
* instance.
49+
* <p>Use static factory methods {@link #create()} or {@link #create(String)},
50+
* or {@link WebClient#builder()} to prepare an instance.
5451
*
55-
* <p>For examples with a response body, see the Javadoc for:
52+
* <p>For examples with a response body see:
5653
* <ul>
5754
* <li>{@link RequestHeadersSpec#retrieve() retrieve()}
5855
* <li>{@link RequestHeadersSpec#exchange() exchange()}
5956
* </ul>
60-
* For examples with a request body see:
57+
* <p>For examples with a request body see:
6158
* <ul>
6259
* <li>{@link RequestBodySpec#body(Publisher, Class) body(Publisher,Class)}
6360
* <li>{@link RequestBodySpec#syncBody(Object) syncBody(Object)}

src/docs/asciidoc/integration.adoc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -950,14 +950,18 @@ plugging in third-party or custom solutions here.
950950

951951
The Spring Framework provides two choices for making calls to REST endpoints:
952952

953-
* <<rest-resttemplate>> -- the original Spring REST client with an API similar to other
954-
template classes in Spring such as `JdbcTemplate`, `JmsTemplate` and others.
955-
`RestTemplate` has a synchronous API and relies on blocking I/O which is okay for
956-
client scenarios with low concurrency.
957-
* <<web-reactive.adoc#webflux-client,WebClient>> -- reactive client with a functional,
958-
fluent API from the `spring-webflux` module. It relies on non-blocking I/O which allows it
959-
to support high concurrency more efficiently (i.e. using a small number of threads) than the
960-
`RestTemplate`. `WebClient` is a natural fit for streaming scenarios.
953+
* <<rest-resttemplate>> -- the original Spring REST client with a synchronous, template
954+
method API.
955+
* <<web-reactive.adoc#webflux-client,WebClient>> -- non-blocking, reactive alternative
956+
that supports both sync and async, as well as streaming scenarios.
957+
958+
[NOTE]
959+
====
960+
As of 5.0, the non-blocking, reactive `WebClient` offers a modern alternative to the
961+
`RestTemplate` with efficient support for both sync and async, as well as streaming
962+
scenarios. The `RestTemplate` will be deprecated in a future version and will not have
963+
major new features gong forward.
964+
====
961965

962966

963967
[[rest-resttemplate]]

src/docs/asciidoc/web/webmvc-client.adoc

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,28 @@ This section describes options for client-side access to REST endpoints.
99
[[webmvc-resttemplate]]
1010
== RestTemplate
1111

12-
`RestTemplate` is the original Spring REST client that follows a similar approach to other
13-
template classes in the Spring Framework (e.g. `JdbcTemplate`, `JmsTemplate`, etc.) by
14-
providing a list of parameterizable methods to perform HTTP requests.
12+
`RestTemplate` is a synchronous client to perform HTTP requests. It is the original
13+
Spring REST client, exposing a simple, template method API over underlying HTTP client
14+
libraries.
1515

16-
`RestTemplate` has a synchronous API and relies on blocking I/O. This is okay for
17-
client scenarios with low concurrency. In a server environment or when orchestrating a
18-
sequence of remote calls, prefer using the `WebClient` which provides a more efficient
19-
execution model including seamless support for streaming.
16+
[NOTE]
17+
====
18+
As of 5.0, the non-blocking, reactive `WebClient` offers a modern alternative to the
19+
`RestTemplate` with efficient support for both sync and async, as well as streaming
20+
scenarios. The `RestTemplate` will be deprecated in a future version and will not have
21+
major new features gong forward.
22+
====
2023

21-
See <<integration.adoc#rest-client-access,RestTemplate>> for more details on using the
22-
`RestTemplate`.
24+
See <<integration.adoc#rest-client-access,RestTemplate>> for details.
2325

2426

2527

2628

2729
[[webmvc-webclient]]
2830
== WebClient
2931

30-
`WebClient` is a reactive client that provides an alternative to the `RestTemplate`. It
31-
exposes a functional, fluent API and relies on non-blocking I/O which allows it to support
32-
high concurrency more efficiently (i.e. using a small number of threads) than the
33-
`RestTemplate`. `WebClient` is a natural fit for streaming scenarios.
32+
`WebClient` is a non-blocking, reactive client to perform HTTP requests. It was
33+
introduced in 5.0 and offers a modern alternative to the `RestTemplate` with efficient
34+
support for both synchronous and asynchronous, as well as streaming scenarios.
3435

35-
See <<web-reactive.adoc#webflux-client,WebClient>> for more details on using the `WebClient`.
36+
See <<web-reactive.adoc#webflux-client,WebClient>> for more details.

0 commit comments

Comments
 (0)