Package org.cibseven.webapp.rest
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
ConstructorsConstructorDescriptionDefault 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 TypeMethodDescriptionaddConverter(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 CustomRestTemplatecreate(RestTemplateConfiguration config) Static factory method to create a fully configured CustomRestTemplate.static CustomRestTemplatecreate(RestTemplateConfiguration config, Object meterRegistry) Static factory method to create a fully configured CustomRestTemplate.voiddestroy()Clean up resources when the application is shutting down.Get the connection pool statistics.voidInitialize 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, setUriTemplateHandlerMethods inherited from class org.springframework.http.client.support.InterceptingHttpAccessor
getInterceptors, getRequestFactory, setInterceptors, setRequestFactoryMethods inherited from class org.springframework.http.client.support.HttpAccessor
createRequest, getClientHttpRequestInitializers, setClientHttpRequestInitializers
-
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
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
Static factory method to create a fully configured CustomRestTemplate. This method encapsulates all the connection/HttpClient creation logic.- Parameters:
config- the configuration to usemeterRegistry- 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
Get the connection pool statistics.- Returns:
- a string representation of the connection pool statistics
-