Skip to content

RestClient - Add support for asynchronous calls #34290

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

Closed
ThomasVitale opened this issue Jan 20, 2025 · 4 comments
Closed

RestClient - Add support for asynchronous calls #34290

ThomasVitale opened this issue Jan 20, 2025 · 4 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@ThomasVitale
Copy link

The RestClient provides a very convenient API on top of HTTP Clients implementations from OpenJDK, Apache, Jetty, and Netty. All those HTTP Clients provide support for performing asynchronous calls. However, the RestClient APIs do not support asynchronous behavior.

The current alternative is skipping Spring and using the underlying HTTP Client library directly or switching to reactive.

I would like to suggest extending the RestClient to support asynchronous calls, and I'm available to help out anyway I can.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 20, 2025
@bclozel bclozel self-assigned this Jan 20, 2025
@bclozel
Copy link
Member

bclozel commented Jan 20, 2025

Hello @ThomasVitale , thanks for reaching out. I have discussed this issue with @sdeleuze and he shared some additional context for this issue. This calls for a broader response so I'll start with that.

We have designed RestClient as a modern developer API, but still reusing the same infrastructure as RestTemplate for issuing requests, decoding content and intercepting exchanges. If we want to handle asynchronous calls properly, we would need a new base infrastructure that mirrors what our reactive HTTP layer does. This calls for a brand new HTTP client, the fourth one in Spring Framework...

HTTP client libraries do support asynchronous calls, but in different ways. For example, the JDK's HttpClient supports java.util.concurrent.Flow, Jetty uses callbacks or reactive streams, HttpComponents uses Future, etc. The Java platform team has been working on Virtual Threads and Structured Concurrency. If we were to consider another client, I think this would be an obvious path to explore. Unfortunately, it's too soon to consider this work stream in Spring Framework. While implementations are pretty solid at this stage, the Java ecosystem is far from being aligned there. Also, we know very little about the expectations of developers. Should we support backpressure and how? What's the expected user-facing API for scatter-gather, timeout, streaming scenarios? Should we consider deprecating one or more existing HTTP clients in our codebase as a result?

WebClient is pretty solid in that space and has been widely adopted. I understand that the reactive APIs are quite viral and that adopting them in libraries is not as easy as in applications. If you are working on a neutral library, maybe using JDK's HttpClient is the safest bet at this point.

We will reconsider this in the future but in the meantime I will close this issue as we have nothing actionable on this right now.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jan 20, 2025
@bclozel bclozel added in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 20, 2025
@tzolov
Copy link

tzolov commented Jan 20, 2025

As I've been struggling with this issue in the SpringAI and MCP projects, I can second @ThomasVitale's suggestion.

RestClient's lack of async support makes it incompatible with Server-Sent Events (SSE), which is crucial as virtually all GenAI APIs use SSE for streaming responses. While I've implemented a custom HttpClient-based solution (FlowSseClient) for MCP, this workaround isn't suitable for Spring AI's broader needs.

Using WebClient with its heavy, coupled client/server dependencies and auto-configurations is an unfortunate design choice that significantly impacts the Spring AI architecture and user experience.

@bclozel
Copy link
Member

bclozel commented Jan 20, 2025

Thanks for the feedback @tzolov . Spring Framework structured this in order to avoid tangles and split packages and for many other reasons. I agree that the effect on auto-configurations is not ideal. The Spring Boot team is considering splitting auto-configurations and creating dedicated starters for clients.

@ThomasVitale
Copy link
Author

@bclozel Thanks for your answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

4 participants