Articles Recommendation and Summary for Spring RestTemplate Configuration

I will recommend some articles to help you config RestTemplate in Spring Boot and get a better understanding of it, like how to add an error handler and manage a connection pool.

Usually, RestTemplate need to config its HTTP client, request factory, error handlers, interceptors, and HTTP client is the most important part.

Articles Recommendation

  1. 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
  2. Apache HttpClient Connection Management: offers a detailed guide to managing connection pool with BasicHttpClientConnectionManager and PoolingHttpClientConnectionManager , including:

    • Configure the Connection Manager
    • Connection Keep-Alive Strategy
    • Connection Persistence/Reuse
    • Configuring Timeouts
    • Connection Eviction
    • Connection Closing
  3. RestTemplate will be marked as deprecated: one paragraph in this article gives a comparison between WebClient and RestTemplate, and points out that WebClient is the future, and RestTemplate will be in maintenance mode.

  4. 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

  1. RestTemplate is a synchronous client to perform HTTP requests.

  2. 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 replace HttpURLConnection.

  3. 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]

References


  1. Using RestTemplate with Apaches HttpClient ↩︎

  2. Apache HttpClient Connection Management ↩︎