diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/MvcUtils.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/MvcUtils.java index 1afb1c52cc..afcd4023aa 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/MvcUtils.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/MvcUtils.java @@ -102,8 +102,18 @@ public static Optional cacheAndReadBody(ServerRequest request, Class t public static ByteArrayInputStream cacheBody(ServerRequest request) { try { byte[] bytes = StreamUtils.copyToByteArray(request.servletRequest().getInputStream()); - ByteArrayInputStream body = new ByteArrayInputStream(bytes); - putAttribute(request, MvcUtils.CACHED_REQUEST_BODY_ATTR, body); + ByteArrayInputStream body; + if (bytes.length > 0) { + body = new ByteArrayInputStream(bytes); + putAttribute(request, MvcUtils.CACHED_REQUEST_BODY_ATTR, body); + } + else { + body = getAttribute(request, CACHED_REQUEST_BODY_ATTR); + if (body == null) { + body = new ByteArrayInputStream(bytes); + putAttribute(request, MvcUtils.CACHED_REQUEST_BODY_ATTR, body); + } + } return body; } catch (IOException e) { diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctions.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctions.java index c7fd9acb5e..96fc9506c4 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctions.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/RetryFilterFunctions.java @@ -43,6 +43,8 @@ import org.springframework.web.servlet.function.ServerRequest; import org.springframework.web.servlet.function.ServerResponse; +import static org.springframework.cloud.gateway.server.mvc.filter.BodyFilterFunctions.adaptCachedBody; + public abstract class RetryFilterFunctions { private RetryFilterFunctions() { @@ -75,7 +77,7 @@ public static HandlerFilterFunction retry(RetryC if (isRetryableStatusCode(serverResponse.statusCode(), config) && isRetryableMethod(request.method(), config)) { // use this to transfer information to HttpStatusRetryPolicy - throw new RetryException(request, serverResponse); + throw new RetryException(adaptCachedBody().apply(request), serverResponse); } return serverResponse; }); diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java index 1f24992472..5e6ad53364 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/handler/RestClientProxyExchange.java @@ -24,6 +24,8 @@ import org.springframework.web.client.RestClient; import org.springframework.web.servlet.function.ServerResponse; +import static org.springframework.cloud.gateway.server.mvc.common.MvcUtils.cacheBody; + public class RestClientProxyExchange implements ProxyExchange { private final RestClient restClient; @@ -41,7 +43,7 @@ public ServerResponse exchange(Request request) { } private static int copyBody(Request request, OutputStream outputStream) throws IOException { - return StreamUtils.copy(request.getServerRequest().servletRequest().getInputStream(), outputStream); + return StreamUtils.copy(cacheBody(request.getServerRequest()), outputStream); } private static ServerResponse doExchange(Request request, ClientHttpResponse clientResponse) throws IOException {