Affects: 6.1


I recently started migrating in a project from RestTemplate to RestClient. I used the RestClient builder as follows:

    RestClient restClient = RestClient.builder()
        .requestFactory(new SimpleClientHttpRequestFactory())
        .requestInterceptor(new BasicAuthenticationInterceptor("username","password"))
        .build();

I then tried to continue, using the Rest client for uploading larger files (~300MB) as a FileInputStream to an online service like this:

        restClient
            .post()
            .uri(URL)
            .body(new InputStreamResource(inputStream))
            .retrieve()
            .toEntity(Object.class);

Soon i realised, the RestClient is holding the whole file in memory, increasing the risk of OOMExceptions.

After a lot of debugging the wrong files, i stumbled accrosse the function: DefaultRestClient.createRequest(URI uri). I realised that it should not be allowed to specify both, RequestInterceptors and RequestFactories.

Hence first, the question why it is supported, although the defined RequestFactory will not be used and second, wouldn't it be more better to either disallow specifying both (following the fail fast principle) or at least documenting that a RequestFactory will not be used, if a RequestInterceptor is specified.

Comment From: bclozel

Hello @hassberg, thanks for reaching out.

Soon i realised, the RestClient is holding the whole file in memory, increasing the risk of OOMExceptions.

Isn't it the case already with RestTemplate? RestClient and RestTemplate do share the same underlying infrastructure. For streaming and asynchronous cases, WebClient is a better fit.

After a lot of debugging the wrong files, i stumbled accrosse the function: DefaultRestClient.createRequest(URI uri). I realised that it should not be allowed to specify both, RequestInterceptors and RequestFactories.

You are allowed to use both because they fulfill different needs: the factory creates the requets using the HTTP library and the interceptors allow to enhance/filter exchanges. Just like with RestTemplate, the configured request factory is wrapped with an InterceptingClientHttpRequestFactory if interceptors were configured. This is an implementation detail and doesn't mean that one excludes the other.

If you have a concrete failure to share, please provide a sample application that demonstrates the problem and we'll have a look. In the meantime, I'm closing this issue as it seems this was a misunderstanding in the first place.