Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TradeOff between creating a new RestTemplate each Time and Creating a Single Class returning same Rest Template

In Spring Boot,for making a Rest Client call currently creating RestTemplate using the new keyword in multiple classes.

RestTemplate restTemplate = new RestTemplate(); 
ResponseEntity<String> response = restTemplate.exchange(

Planning to create a single class that returns a same instance of RestTemplate, and using it for all the Rest calls.

Will it affect performance. What may be the drawbacks on performance or any other?

Also instead of creating a single RestTemplate, is using Pooling a better option? Thanks

like image 682
Rehan Avatar asked May 30 '16 04:05

Rehan


1 Answers

Creating a new RestTemplate every time you need to use it will be more expensive then just creating it once and using dependency injection to get a reference to it.

Creating a connection pool would provide an additional performance boost because it would allow connections to be re-used (if that is what you need)

like image 138
mad_fox Avatar answered Nov 30 '22 04:11

mad_fox