I am using spring 3.0.5 version of RestTemplate Can any one help me .how to set connecttimeout and readTimeout values for each request
but in latest versions there is a solution with HttpComponentsClientHttpRequestFactory class .but i need with spring.web.3.0.5 version
private void setTimeout(RestTemplate restTemplate, int timeout) {
//Explicitly setting ClientHttpRequestFactory instance to
//SimpleClientHttpRequestFactory instance to leverage
//set*Timeout methods
restTemplate.setRequestFactory(new SimpleClientHttpRequestFactory());
SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate
.getRequestFactory();
rf.setReadTimeout(timeout);
rf.setConnectTimeout(timeout);
}
I am using Spring 5.2 and got clean way of setting read and connect timeout settings for RestTemplate object:
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
.setConnectTimeout(Duration.ofMillis(connectTimeoutMillis))
.setReadTimeout(Duration.ofMillis(readTimeoutMillis))
.build();
}
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
var factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(3000);
factory.setReadTimeout(3000);
return new RestTemplate(factory);
}
}
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