Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set connect/read timeout in the Spring's `RestClient`?

How to set a connect/read timeout in the Spring's RestClient? This client has been added in the Spring Framework 6.1.

like image 318
AlexElin Avatar asked Dec 16 '25 13:12

AlexElin


1 Answers

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;
    }
like image 112
Andrei Lisa Avatar answered Dec 19 '25 07:12

Andrei Lisa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!