How to set a connect/read timeout in the Spring's RestClient? This client has been added in the Spring Framework 6.1.
You could create a HttpComponentsClientHttpRequestFactory where you will set connection and read timeout and then you will be able to set it to RestClient using provided builder.
public Object exampleMethod(Object object) {
RestClient restClient = RestClient
.builder()
.requestFactory(getClientHttpRequestFactory())
.build();
//usage of restClient with timeout.
restClient.get();
}
private ClientHttpRequestFactory getClientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
clientHttpRequestFactory.setConnectTimeout(100);
clientHttpRequestFactory.setConnectionRequestTimeout(70);
return clientHttpRequestFactory;
}
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