Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices on rest client using spring RestTemplate

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.

like image 408
Chamly Idunil Avatar asked Nov 20 '17 09:11

Chamly Idunil


People also ask

How can I improve my RestTemplate performance?

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.

Does RestTemplate use HttpClient?

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.

Which is better RestTemplate or WebClient?

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.

Is RestTemplate being deprecated?

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.


1 Answers

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

like image 55
pvpkiran Avatar answered Sep 19 '22 19:09

pvpkiran