Class CustomRestTemplate

java.lang.Object
org.springframework.http.client.support.HttpAccessor
org.springframework.http.client.support.InterceptingHttpAccessor
org.springframework.web.client.RestTemplate
org.cibseven.webapp.rest.CustomRestTemplate
All Implemented Interfaces:
org.springframework.web.client.RestOperations

public class CustomRestTemplate extends org.springframework.web.client.RestTemplate
Enhanced implementation of RestTemplate that uses Apache HttpClient with connection pooling and can be configured with specific settings for HTTP requests in the application. Features: - Connection pooling with PoolingHttpClientConnectionManager - Configurable connection and read timeouts - Configurable keep-alive and connection reuse strategies - Support for request interceptors and custom HttpMessageConverters - Optional integration with Micrometer for metrics
  • Field Summary

    Fields inherited from class org.springframework.http.client.support.HttpAccessor

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor that sets up a RestTemplate with default configuration.
    CustomRestTemplate(org.springframework.http.client.ClientHttpRequestFactory requestFactory)
    Constructor that accepts a custom ClientHttpRequestFactory.
  • Method Summary

    Modifier and Type
    Method
    Description
    addConverter(org.springframework.http.converter.HttpMessageConverter<?> converter)
    Add a custom message converter to the RestTemplate.
    addInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor interceptor)
    Add a request interceptor to the RestTemplate.
    Static factory method to create a fully configured CustomRestTemplate.
    create(RestTemplateConfiguration config, Object meterRegistry)
    Static factory method to create a fully configured CustomRestTemplate.
    void
    Clean up resources when the application is shutting down.
    Get the connection pool statistics.
    void
    Initialize the RestTemplate with the configured settings.

    Methods inherited from class org.springframework.web.client.RestTemplate

    acceptHeaderRequestCallback, delete, delete, delete, doExecute, doExecute, exchange, exchange, exchange, exchange, exchange, exchange, exchange, exchange, execute, execute, execute, getErrorHandler, getForEntity, getForEntity, getForEntity, getForObject, getForObject, getForObject, getMessageConverters, getObservationConvention, getObservationRegistry, getUriTemplateHandler, handleResponse, headersExtractor, headForHeaders, headForHeaders, headForHeaders, httpEntityCallback, httpEntityCallback, optionsForAllow, optionsForAllow, optionsForAllow, patchForObject, patchForObject, patchForObject, postForEntity, postForEntity, postForEntity, postForLocation, postForLocation, postForLocation, postForObject, postForObject, postForObject, put, put, put, responseEntityExtractor, setDefaultUriVariables, setErrorHandler, setMessageConverters, setObservationConvention, setObservationRegistry, setUriTemplateHandler

    Methods inherited from class org.springframework.http.client.support.InterceptingHttpAccessor

    getInterceptors, getRequestFactory, setInterceptors, setRequestFactory

    Methods inherited from class org.springframework.http.client.support.HttpAccessor

    createRequest, getClientHttpRequestInitializers, setClientHttpRequestInitializers

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CustomRestTemplate

      public CustomRestTemplate()
      Default constructor that sets up a RestTemplate with default configuration. Configuration will be applied during @PostConstruct.
    • CustomRestTemplate

      public CustomRestTemplate(org.springframework.http.client.ClientHttpRequestFactory requestFactory)
      Constructor that accepts a custom ClientHttpRequestFactory. This constructor is mainly for backward compatibility.
      Parameters:
      requestFactory - the request factory to use
  • Method Details

    • create

      public static CustomRestTemplate create(RestTemplateConfiguration config)
      Static factory method to create a fully configured CustomRestTemplate. This method encapsulates all the connection/HttpClient creation logic.
      Parameters:
      config - the configuration to use
      Returns:
      a configured CustomRestTemplate instance
    • create

      public static CustomRestTemplate create(RestTemplateConfiguration config, Object meterRegistry)
      Static factory method to create a fully configured CustomRestTemplate. This method encapsulates all the connection/HttpClient creation logic.
      Parameters:
      config - the configuration to use
      meterRegistry - optional meter registry for metrics
      Returns:
      a configured CustomRestTemplate instance
    • addInterceptor

      public CustomRestTemplate addInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor interceptor)
      Add a request interceptor to the RestTemplate.
      Parameters:
      interceptor - the interceptor to add
      Returns:
      this CustomRestTemplate instance for method chaining
    • addConverter

      public CustomRestTemplate addConverter(org.springframework.http.converter.HttpMessageConverter<?> converter)
      Add a custom message converter to the RestTemplate.
      Parameters:
      converter - the converter to add
      Returns:
      this CustomRestTemplate instance for method chaining
    • initialize

      @PostConstruct public void initialize()
      Initialize the RestTemplate with the configured settings. This method is called automatically by Spring after dependency injection.
    • destroy

      public void destroy()
      Clean up resources when the application is shutting down.
    • getConnectionPoolStats

      public String getConnectionPoolStats()
      Get the connection pool statistics.
      Returns:
      a string representation of the connection pool statistics