Skip to content

Commit 27c4f0e

Browse files
committed
Built-in buffering support in RestClient
Closes gh-33785
1 parent 6a0c5dd commit 27c4f0e

File tree

7 files changed

+136
-16
lines changed

7 files changed

+136
-16
lines changed

spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestFactory.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,17 +18,22 @@
1818

1919
import java.io.IOException;
2020
import java.net.URI;
21+
import java.util.function.BiPredicate;
2122

2223
import org.springframework.http.HttpMethod;
2324

2425
/**
25-
* Wrapper for a {@link ClientHttpRequestFactory} that buffers
26-
* all outgoing and incoming streams in memory.
26+
* {@link ClientHttpRequestFactory} that wraps another in order to buffer
27+
* outgoing and incoming content in memory, making it possible to set a
28+
* content-length on the request, and to read the
29+
* {@linkplain ClientHttpResponse#getBody() response body} multiple times.
2730
*
28-
* <p>Using this wrapper allows for multiple reads of the
29-
* {@linkplain ClientHttpResponse#getBody() response body}.
31+
* <p><strong>Note:</strong> as of 7.0, buffering can be enabled through
32+
* {@link org.springframework.web.client.RestClient.Builder#bufferContent(BiPredicate)}
33+
* and therefore it is not necessary for applications to use this class directly.
3034
*
3135
* @author Arjen Poutsma
36+
* @author Rossen Stoyanchev
3237
* @since 3.1
3338
*/
3439
public class BufferingClientHttpRequestFactory extends AbstractClientHttpRequestFactoryWrapper {

spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.net.URI;
2121
import java.util.List;
22+
import java.util.function.BiPredicate;
2223

2324
import org.springframework.http.HttpHeaders;
2425
import org.springframework.http.HttpMethod;
@@ -42,14 +43,18 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
4243

4344
private final URI uri;
4445

46+
private final BiPredicate<URI, HttpMethod> bufferingPredicate;
47+
4548

4649
protected InterceptingClientHttpRequest(ClientHttpRequestFactory requestFactory,
47-
List<ClientHttpRequestInterceptor> interceptors, URI uri, HttpMethod method) {
50+
List<ClientHttpRequestInterceptor> interceptors, URI uri, HttpMethod method,
51+
BiPredicate<URI, HttpMethod> bufferingPredicate) {
4852

4953
this.requestFactory = requestFactory;
5054
this.interceptors = interceptors;
5155
this.method = method;
5256
this.uri = uri;
57+
this.bufferingPredicate = bufferingPredicate;
5358
}
5459

5560

@@ -76,6 +81,10 @@ private ClientHttpRequestExecution getExecution() {
7681
.orElse(execution);
7782
}
7883

84+
private boolean shouldBufferResponse(HttpRequest request) {
85+
return this.bufferingPredicate.test(request.getURI(), request.getMethod());
86+
}
87+
7988

8089
private class EndOfChainRequestExecution implements ClientHttpRequestExecution {
8190

@@ -90,7 +99,7 @@ public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOExc
9099
ClientHttpRequest delegate = this.requestFactory.createRequest(request.getURI(), request.getMethod());
91100
request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
92101
request.getAttributes().forEach((key, value) -> delegate.getAttributes().put(key, value));
93-
return executeWithRequest(delegate, body, false);
102+
return executeWithRequest(delegate, body, shouldBufferResponse(request));
94103
}
95104
}
96105

spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequestFactory.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.net.URI;
2020
import java.util.Collections;
2121
import java.util.List;
22+
import java.util.function.BiPredicate;
2223

2324
import org.jspecify.annotations.Nullable;
2425

@@ -29,6 +30,7 @@
2930
* {@link ClientHttpRequestInterceptor ClientHttpRequestInterceptors}.
3031
*
3132
* @author Arjen Poutsma
33+
* @author Rossen Stoyanchev
3234
* @since 3.1
3335
* @see ClientHttpRequestFactory
3436
* @see ClientHttpRequestInterceptor
@@ -37,23 +39,41 @@ public class InterceptingClientHttpRequestFactory extends AbstractClientHttpRequ
3739

3840
private final List<ClientHttpRequestInterceptor> interceptors;
3941

42+
private final BiPredicate<URI, HttpMethod> bufferingPredicate;
43+
4044

4145
/**
42-
* Create a new instance of the {@code InterceptingClientHttpRequestFactory} with the given parameters.
46+
* Create a new instance with the given parameters.
4347
* @param requestFactory the request factory to wrap
4448
* @param interceptors the interceptors that are to be applied (can be {@code null})
4549
*/
4650
public InterceptingClientHttpRequestFactory(ClientHttpRequestFactory requestFactory,
4751
@Nullable List<ClientHttpRequestInterceptor> interceptors) {
4852

53+
this(requestFactory, interceptors, null);
54+
}
55+
56+
/**
57+
* Constructor variant with an additional predicate to decide whether to
58+
* buffer the response.
59+
* @since 7.0
60+
*/
61+
public InterceptingClientHttpRequestFactory(ClientHttpRequestFactory requestFactory,
62+
@Nullable List<ClientHttpRequestInterceptor> interceptors,
63+
@Nullable BiPredicate<URI, HttpMethod> bufferingPredicate) {
64+
4965
super(requestFactory);
5066
this.interceptors = (interceptors != null ? interceptors : Collections.emptyList());
67+
this.bufferingPredicate = (bufferingPredicate != null ? bufferingPredicate : (uri, method) -> false);
5168
}
5269

5370

5471
@Override
55-
protected ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod, ClientHttpRequestFactory requestFactory) {
56-
return new InterceptingClientHttpRequest(requestFactory, this.interceptors, uri, httpMethod);
72+
protected ClientHttpRequest createRequest(
73+
URI uri, HttpMethod httpMethod, ClientHttpRequestFactory requestFactory) {
74+
75+
return new InterceptingClientHttpRequest(
76+
requestFactory, this.interceptors, uri, httpMethod, this.bufferingPredicate);
5777
}
5878

5979
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.concurrent.ConcurrentHashMap;
32+
import java.util.function.BiPredicate;
3233
import java.util.function.Consumer;
3334
import java.util.function.Function;
3435
import java.util.function.Predicate;
@@ -48,6 +49,7 @@
4849
import org.springframework.http.MediaType;
4950
import org.springframework.http.ResponseEntity;
5051
import org.springframework.http.StreamingHttpOutputMessage;
52+
import org.springframework.http.client.BufferingClientHttpRequestFactory;
5153
import org.springframework.http.client.ClientHttpRequest;
5254
import org.springframework.http.client.ClientHttpRequestFactory;
5355
import org.springframework.http.client.ClientHttpRequestInitializer;
@@ -75,6 +77,7 @@
7577
*
7678
* @author Arjen Poutsma
7779
* @author Sebastien Deleuze
80+
* @author Rossen Stoyanchev
7881
* @since 6.1
7982
* @see RestClient#create()
8083
* @see RestClient#create(String)
@@ -97,6 +100,8 @@ final class DefaultRestClient implements RestClient {
97100

98101
private final @Nullable List<ClientHttpRequestInterceptor> interceptors;
99102

103+
private final @Nullable BiPredicate<URI, HttpMethod> bufferingPredicate;
104+
100105
private final UriBuilderFactory uriBuilderFactory;
101106

102107
private final @Nullable HttpHeaders defaultHeaders;
@@ -118,6 +123,7 @@ final class DefaultRestClient implements RestClient {
118123

119124
DefaultRestClient(ClientHttpRequestFactory clientRequestFactory,
120125
@Nullable List<ClientHttpRequestInterceptor> interceptors,
126+
@Nullable BiPredicate<URI, HttpMethod> bufferingPredicate,
121127
@Nullable List<ClientHttpRequestInitializer> initializers,
122128
UriBuilderFactory uriBuilderFactory,
123129
@Nullable HttpHeaders defaultHeaders,
@@ -132,6 +138,7 @@ final class DefaultRestClient implements RestClient {
132138
this.clientRequestFactory = clientRequestFactory;
133139
this.initializers = initializers;
134140
this.interceptors = interceptors;
141+
this.bufferingPredicate = bufferingPredicate;
135142
this.uriBuilderFactory = uriBuilderFactory;
136143
this.defaultHeaders = defaultHeaders;
137144
this.defaultCookies = defaultCookies;
@@ -643,10 +650,14 @@ private ClientHttpRequest createRequest(URI uri) throws IOException {
643650
factory = DefaultRestClient.this.interceptingRequestFactory;
644651
if (factory == null) {
645652
factory = new InterceptingClientHttpRequestFactory(
646-
DefaultRestClient.this.clientRequestFactory, DefaultRestClient.this.interceptors);
653+
DefaultRestClient.this.clientRequestFactory, DefaultRestClient.this.interceptors,
654+
DefaultRestClient.this.bufferingPredicate);
647655
DefaultRestClient.this.interceptingRequestFactory = factory;
648656
}
649657
}
658+
else if (DefaultRestClient.this.bufferingPredicate != null) {
659+
factory = new BufferingClientHttpRequestFactory(DefaultRestClient.this.clientRequestFactory);
660+
}
650661
else {
651662
factory = DefaultRestClient.this.clientRequestFactory;
652663
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,13 +23,15 @@
2323
import java.util.LinkedHashMap;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.function.BiPredicate;
2627
import java.util.function.Consumer;
2728
import java.util.function.Predicate;
2829

2930
import io.micrometer.observation.ObservationRegistry;
3031
import org.jspecify.annotations.Nullable;
3132

3233
import org.springframework.http.HttpHeaders;
34+
import org.springframework.http.HttpMethod;
3335
import org.springframework.http.HttpStatusCode;
3436
import org.springframework.http.client.ClientHttpRequestFactory;
3537
import org.springframework.http.client.ClientHttpRequestInitializer;
@@ -137,6 +139,8 @@ final class DefaultRestClientBuilder implements RestClient.Builder {
137139

138140
private @Nullable List<ClientHttpRequestInterceptor> interceptors;
139141

142+
private @Nullable BiPredicate<URI, HttpMethod> bufferingPredicate;
143+
140144
private @Nullable List<ClientHttpRequestInitializer> initializers;
141145

142146
private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;
@@ -172,6 +176,7 @@ public DefaultRestClientBuilder(DefaultRestClientBuilder other) {
172176
new ArrayList<>(other.messageConverters) : null);
173177

174178
this.interceptors = (other.interceptors != null) ? new ArrayList<>(other.interceptors) : null;
179+
this.bufferingPredicate = other.bufferingPredicate;
175180
this.initializers = (other.initializers != null) ? new ArrayList<>(other.initializers) : null;
176181
this.observationRegistry = other.observationRegistry;
177182
this.observationConvention = other.observationConvention;
@@ -347,6 +352,12 @@ private List<ClientHttpRequestInterceptor> initInterceptors() {
347352
return this.interceptors;
348353
}
349354

355+
@Override
356+
public RestClient.Builder bufferContent(BiPredicate<URI, HttpMethod> predicate) {
357+
this.bufferingPredicate = predicate;
358+
return this;
359+
}
360+
350361
@Override
351362
public RestClient.Builder requestInitializer(ClientHttpRequestInitializer initializer) {
352363
Assert.notNull(initializer, "Initializer must not be null");
@@ -463,7 +474,7 @@ public RestClient build() {
463474
(this.messageConverters != null ? this.messageConverters : initMessageConverters());
464475

465476
return new DefaultRestClient(
466-
requestFactory, this.interceptors, this.initializers,
477+
requestFactory, this.interceptors, this.bufferingPredicate, this.initializers,
467478
uriBuilderFactory, defaultHeaders, defaultCookies,
468479
this.defaultRequest,
469480
this.statusHandlers,

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import java.time.ZonedDateTime;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.function.BiPredicate;
2627
import java.util.function.Consumer;
2728
import java.util.function.Function;
2829
import java.util.function.Predicate;
@@ -384,6 +385,15 @@ Builder defaultStatusHandler(Predicate<HttpStatusCode> statusPredicate,
384385
*/
385386
Builder requestInterceptors(Consumer<List<ClientHttpRequestInterceptor>> interceptorsConsumer);
386387

388+
/**
389+
* Enable buffering of request and response content making it possible to
390+
* read the request and the response body multiple times.
391+
* @param predicate to determine whether to buffer for the given request
392+
* @return this builder
393+
* @since 7.0
394+
*/
395+
Builder bufferContent(BiPredicate<URI, HttpMethod> predicate);
396+
387397
/**
388398
* Add the given request initializer to the end of the initializer chain.
389399
* @param initializer the initializer to be added to the chain

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.springframework.http.client.ReactorClientHttpRequestFactory;
5252
import org.springframework.http.client.SimpleClientHttpRequestFactory;
5353
import org.springframework.util.CollectionUtils;
54+
import org.springframework.util.FileCopyUtils;
5455
import org.springframework.util.LinkedMultiValueMap;
5556
import org.springframework.util.MultiValueMap;
5657
import org.springframework.web.testfixture.xml.Pojo;
@@ -811,6 +812,59 @@ void requestInterceptor(ClientHttpRequestFactory requestFactory) {
811812
expectRequest(request -> assertThat(request.getHeader("foo")).isEqualTo("bar"));
812813
}
813814

815+
@ParameterizedRestClientTest
816+
void requestInterceptorWithResponseBuffering(ClientHttpRequestFactory requestFactory) {
817+
startServer(requestFactory);
818+
819+
prepareResponse(response ->
820+
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
821+
822+
RestClient interceptedClient = this.restClient.mutate()
823+
.requestInterceptor((request, body, execution) -> {
824+
ClientHttpResponse response = execution.execute(request, body);
825+
byte[] result = FileCopyUtils.copyToByteArray(response.getBody());
826+
assertThat(result).isEqualTo("Hello Spring!".getBytes(UTF_8));
827+
return response;
828+
})
829+
.bufferContent((uri, httpMethod) -> true)
830+
.build();
831+
832+
String result = interceptedClient.get()
833+
.uri("/greeting")
834+
.retrieve()
835+
.body(String.class);
836+
837+
assertThat(result).isEqualTo("Hello Spring!");
838+
839+
expectRequestCount(1);
840+
}
841+
842+
@ParameterizedRestClientTest
843+
void bufferContent(ClientHttpRequestFactory requestFactory) {
844+
startServer(requestFactory);
845+
846+
prepareResponse(response ->
847+
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
848+
849+
RestClient bufferingClient = this.restClient.mutate()
850+
.bufferContent((uri, httpMethod) -> true)
851+
.build();
852+
853+
String result = bufferingClient.get()
854+
.uri("/greeting")
855+
.exchange((request, response) -> {
856+
byte[] bytes = FileCopyUtils.copyToByteArray(response.getBody());
857+
assertThat(bytes).isEqualTo("Hello Spring!".getBytes(UTF_8));
858+
bytes = FileCopyUtils.copyToByteArray(response.getBody());
859+
assertThat(bytes).isEqualTo("Hello Spring!".getBytes(UTF_8));
860+
return new String(bytes, UTF_8);
861+
});
862+
863+
assertThat(result).isEqualTo("Hello Spring!");
864+
865+
expectRequestCount(1);
866+
}
867+
814868
@ParameterizedRestClientTest
815869
void retrieveDefaultCookiesAsCookieHeader(ClientHttpRequestFactory requestFactory) {
816870
startServer(requestFactory);

0 commit comments

Comments
 (0)