Skip to content

Commit 8d6117e

Browse files
RazorNdrstoyanchev
authored andcommitted
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 <[email protected]>
1 parent 2477544 commit 8d6117e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,14 @@ public <T> RequestBodySpec body(T body, ParameterizedTypeReference<T> bodyType)
466466

467467
@Override
468468
public RequestBodySpec body(StreamingHttpOutputMessage.Body body) {
469-
this.body = request -> body.writeTo(request.getBody());
469+
this.body = request -> {
470+
if (request instanceof StreamingHttpOutputMessage streamingMessage) {
471+
streamingMessage.setBody(body);
472+
}
473+
else {
474+
body.writeTo(request.getBody());
475+
}
476+
};
470477
return this;
471478
}
472479

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.client;
1818

19+
import java.io.ByteArrayInputStream;
1920
import java.io.IOException;
2021
import java.lang.annotation.ElementType;
2122
import java.lang.annotation.Retention;
@@ -529,6 +530,27 @@ void postPojoAsJson(ClientHttpRequestFactory requestFactory) {
529530
});
530531
}
531532

533+
@ParameterizedRestClientTest
534+
void postStreamingMessageBody(ClientHttpRequestFactory requestFactory) {
535+
startServer(requestFactory);
536+
537+
prepareResponse(response -> response.setResponseCode(200));
538+
539+
ResponseEntity<Void> result = this.restClient.post()
540+
.uri("/streaming/body")
541+
.body(new ByteArrayInputStream("test-data".getBytes(UTF_8))::transferTo)
542+
.retrieve()
543+
.toBodilessEntity();
544+
545+
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
546+
547+
expectRequestCount(1);
548+
expectRequest(request -> {
549+
assertThat(request.getPath()).isEqualTo("/streaming/body");
550+
assertThat(request.getBody().readUtf8()).isEqualTo("test-data");
551+
});
552+
}
553+
532554
@ParameterizedRestClientTest // gh-31361
533555
public void postForm(ClientHttpRequestFactory requestFactory) {
534556
startServer(requestFactory);

0 commit comments

Comments
 (0)