Browse Source

Support StreamingHttpOutputMessage in RestClient

This commit allows RestClient to handle StreamingHttpOutputMessage
properly by checking the type of the request and invoking setBody()
when appropriate. This improves interoperability with components that
expect streamed output.

A new integration test has been added to verify the functionality.

See gh-35102

Signed-off-by: Daniil Razorenov <daniltmb@gmail.com>
pull/35405/head
Daniil Razorenov 7 months ago committed by rstoyanchev
parent
commit
8d6117e419
  1. 9
      spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java
  2. 22
      spring-web/src/test/java/org/springframework/web/client/RestClientIntegrationTests.java

9
spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java

@ -466,7 +466,14 @@ final class DefaultRestClient implements RestClient { @@ -466,7 +466,14 @@ final class DefaultRestClient implements RestClient {
@Override
public RequestBodySpec body(StreamingHttpOutputMessage.Body body) {
this.body = request -> body.writeTo(request.getBody());
this.body = request -> {
if (request instanceof StreamingHttpOutputMessage streamingMessage) {
streamingMessage.setBody(body);
}
else {
body.writeTo(request.getBody());
}
};
return this;
}

22
spring-web/src/test/java/org/springframework/web/client/RestClientIntegrationTests.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.web.client;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@ -529,6 +530,27 @@ class RestClientIntegrationTests { @@ -529,6 +530,27 @@ class RestClientIntegrationTests {
});
}
@ParameterizedRestClientTest
void postStreamingMessageBody(ClientHttpRequestFactory requestFactory) {
startServer(requestFactory);
prepareResponse(response -> response.setResponseCode(200));
ResponseEntity<Void> result = this.restClient.post()
.uri("/streaming/body")
.body(new ByteArrayInputStream("test-data".getBytes(UTF_8))::transferTo)
.retrieve()
.toBodilessEntity();
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
expectRequestCount(1);
expectRequest(request -> {
assertThat(request.getPath()).isEqualTo("/streaming/body");
assertThat(request.getBody().readUtf8()).isEqualTo("test-data");
});
}
@ParameterizedRestClientTest // gh-31361
public void postForm(ClientHttpRequestFactory requestFactory) {
startServer(requestFactory);

Loading…
Cancel
Save