Skip to content

Commit 2a44914

Browse files
committed
Fix Bug(Gateway MVC): Make sure Retry filter sends correct body when retrying POST/PUT/etc
1 parent d722898 commit 2a44914

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/MvcUtils.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,14 @@ public static <T> Optional<T> cacheAndReadBody(ServerRequest request, Class<T> t
102102
public static ByteArrayInputStream cacheBody(ServerRequest request) {
103103
try {
104104
byte[] bytes = StreamUtils.copyToByteArray(request.servletRequest().getInputStream());
105-
ByteArrayInputStream body = new ByteArrayInputStream(bytes);
106-
putAttribute(request, MvcUtils.CACHED_REQUEST_BODY_ATTR, body);
105+
ByteArrayInputStream body;
106+
if (bytes.length > 0) {
107+
body = new ByteArrayInputStream(bytes);
108+
putAttribute(request, MvcUtils.CACHED_REQUEST_BODY_ATTR, body);
109+
}
110+
else {
111+
body = getAttribute(request, CACHED_REQUEST_BODY_ATTR);
112+
}
107113
return body;
108114
}
109115
catch (IOException e) {

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctions.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import org.springframework.web.servlet.function.ServerRequest;
4444
import org.springframework.web.servlet.function.ServerResponse;
4545

46+
import static org.springframework.cloud.gateway.server.mvc.filter.BodyFilterFunctions.adaptCachedBody;
47+
4648
public abstract class RetryFilterFunctions {
4749

4850
private RetryFilterFunctions() {
@@ -75,7 +77,7 @@ public static HandlerFilterFunction<ServerResponse, ServerResponse> retry(RetryC
7577
if (isRetryableStatusCode(serverResponse.statusCode(), config)
7678
&& isRetryableMethod(request.method(), config)) {
7779
// use this to transfer information to HttpStatusRetryPolicy
78-
throw new RetryException(request, serverResponse);
80+
throw new RetryException(adaptCachedBody().apply(request), serverResponse);
7981
}
8082
return serverResponse;
8183
});

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.web.client.RestClient;
2525
import org.springframework.web.servlet.function.ServerResponse;
2626

27+
import static org.springframework.cloud.gateway.server.mvc.common.MvcUtils.cacheBody;
28+
2729
public class RestClientProxyExchange implements ProxyExchange {
2830

2931
private final RestClient restClient;
@@ -41,7 +43,7 @@ public ServerResponse exchange(Request request) {
4143
}
4244

4345
private static int copyBody(Request request, OutputStream outputStream) throws IOException {
44-
return StreamUtils.copy(request.getServerRequest().servletRequest().getInputStream(), outputStream);
46+
return StreamUtils.copy(cacheBody(request.getServerRequest()), outputStream);
4547
}
4648

4749
private static ServerResponse doExchange(Request request, ClientHttpResponse clientResponse) throws IOException {

0 commit comments

Comments
 (0)