Articles Recommendation and Summary for Spring RestTemplate Configuration
While learning how to configure RestTemplate, I find there are many things to config, and I lack brief understanding of RestTemplate. After searching and reading, I wrote this article to summary the things I learned to help me remember them.
RestTemplate Configuration Outline
Usually, RestTemplate need to config its HTTP client, request factory, error handlers and interceptors, and HTTP client is the most important part.
- restTemplate configuration
- request factory
- http client
- request config: e.g. connection timeout, request timeout
- socket config: e.g. socket timeout
- connection manager: e.g. max connections, max connections per route, connection reuse/persistence
- keep alive strategy:how long a connection may remain unused in the pool
- connection eviction: evict idle connection, can be achieved by http client or thread monitor
- http client
- errorHandler
- interceptors
- request factory
Articles Recommendation
-
Using RestTemplate with Apaches HttpClient: give a comprehensive explanation and configuration guide to RestTemplate, including:
- Configuration of Apache HttpComponents
- Connection Pool
- Connection Keep-Alive Strategy
- Connection Monitor
- Configuration of RestTemplate
- HTTP Request Factory
- Custom Error Handler
- HTTP Request Interceptor
- Configuration of Apache HttpComponents
-
Apache HttpClient Connection Management: offers a detailed guide to managing connection pool with
BasicHttpClientConnectionManager
andPoolingHttpClientConnectionManager
, including:- Configure the Connection Manager
- Connection Keep-Alive Strategy
- Connection Persistence/Reuse
- Configuring Timeouts
- Connection Eviction
- Connection Closing
-
RestTemplate will be marked as deprecated: one paragraph in this article gives a comparison between
WebClient
andRestTemplate
, and points out thatWebClient
is the future, andRestTemplate
will be in maintenance mode. -
Idle Connection and Expire Connection: the above articles often talk of evicting idle and expired connections. The reason we need to evict them is that connections consume many resources, such as Memory, so we need to efficiently manage connections. An idle connection is a connection that is available to be used in a connection pool, while an expired connection is a connection that has been closed on the server side, but is not closed on the client side.
Summary
-
RestTemplate is a synchronous client to perform HTTP requests.
-
RestTemplate is superior to the HTTP client and takes care of the transformation from JSON or XML to Java objects. The HTTP client, on the other hand, takes care of all low-level details of communication via HTTP.[1] RestTemplate's default HTTP client API is
HttpURLConnection
, and Apache HttpComponents, OkHttp and Netty all can be used in the RestTemplate substructure to replaceHttpURLConnection
. -
RestTemplate is based on a thread-per-request model. Every request to RestTemplate blocks until the response is received. As a result, applications using RestTemplate will not scale well with an increasing number of concurrent users.[2]