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
    • errorHandler
    • interceptors

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 ↩︎