I have read some tutorials about implementing REST client in java web application that use SPRING to manage beans.
Every example I found, every time doing a REST request it creates new RestTemplate.
Normally web applications use singleton spring bean.
So I want to know when what is the best practice to use RestTemplate in Spring configures application ?
Use singleton RestTemplate ?
Create RestTemplate in every request. ?
Please advise and describe all situations.
By using pooling we cut this overhead and instead re-use the already opened HTTP connections. This means that connections don't have to be re-established every time, saving us a lot of overhead and time. Especially the handshake procedure when establishing a connection consumes the most time in relation to the other.
RestTemplate delegates to a ClientHttpRequestFactory, and one of the implementations of this interface uses Apache's HttpClient. So, if the goal is to communicate with a Restful API, and you still want to use HttpClient, you can use RestTemplate over HttpClient.
RestTemplate will still be used. But in some cases, the non-blocking approach uses much fewer system resources compared to the blocking one. So, WebClient is a preferable choice in those cases.
WebClient offers support for both synchronous and asynchronous HTTP requests and streaming scenarios. Therefore, RestTemplate will be marked as deprecated in a future version of the Spring Framework and will not contain any new functionalities. RestTemplate is based on a thread-per-request model.
One of the best ways to do this is to create a bean which would return a RestTemplate and then Autowire it in which ever class you need.
Something like this.
@Configuration
public class ProductServiceConfig {
@Value("${product.username}")
private String productServiceUsername;
@Value("${product.password}")
private String productServicePassword;
@Bean(name = "restTemplateForProductService")
public RestTemplate prepareRestTemplateForProductService() {
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(productServiceUsername, productServicePassword));
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder = requestBuilder.setConnectTimeout(1000);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
httpClientBuilder.setDefaultRequestConfig(requestBuilder.build());
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpComponentsClientHttpRequestFactory rf = new HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(rf);
}
}
This way you can set different parameters that you want for your rest call, like timeouts or credentials etc. And when you want to use, you can just do
@Autowired
RestTemplate restTemplateForProductService;
restTemplateForProductService.......
Another advantage of this over using new RestTemplate ()
is if you have to call different services through REST, then you can define multiple beans (with different configuration) which returns RestTemplates and autowire it using the name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With