Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestClient can not use both parameters, ClientHttpRequestFactory and ClientHttpRequestInterceptor #32773

Closed
hassberg opened this issue May 7, 2024 · 1 comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid

Comments

@hassberg
Copy link

hassberg commented May 7, 2024

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 7, 2024
@bclozel
Copy link
Member

bclozel commented May 7, 2024

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.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale May 7, 2024
@bclozel bclozel added in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants