spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Support OutputStream (or StreamingHttpOutputMessage.Body functional interface) and InputStream in HTTP Interface

Open hostmenow opened this issue 1 year ago • 2 comments

HTTP Interfaces are great but it's impossible to replace pure RestClient calls in all cases. For instance, code like

restClient.put()
                .uri(...)
                .body(body ->  {
                   //use body as OutputStream
                 })
                .retrieve()
                .toBodilessEntity();

or

restClient.get()
                .uri(...)
                .exchange((req, resp) -> {
                    var inputStream = resp.getBody());
                    //use inputStream 
                });

cannot be replaced with HTTP Interface (as I know).

It causes minor inconvenience as HTTP Interface is used in 99% cases but RestClient is still injected to cover cases like download/upload large files without significant memory consumption and all other cases where Stream processing is needed.

Propose to add:

  • OutputStream (or StreamingHttpOutputMessage.Body) as supported method parameter here https://docs.spring.io/spring-framework/reference/integration/rest-clients.html#rest-http-interface-method-parameters
  • InputStream or (ResponseEntity<InputStream>?) as supported return value here https://docs.spring.io/spring-framework/reference/integration/rest-clients.html#rest-http-interface-return-values

hostmenow avatar Mar 04 '24 03:03 hostmenow

I'm in a similar situation. I tried using the ResponseEntity<InputStreamResource> as response type with no luck(stream closed). It seems that the DefaultRestClient unconditionally closes the ClientHttpResponse in twr.

ooraini avatar Apr 25 '24 15:04 ooraini

Is there any update on supporting InputStream? I tried with Resource and InputStream but no luck. We are planning to migrate to the HTTP interface, is there any other alternative to support file download?

kap199297 avatar Aug 18 '24 09:08 kap199297