diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/WebClientWriteResponseFilter.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/WebClientWriteResponseFilter.java index ec348905..63ed4248 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/WebClientWriteResponseFilter.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/WebClientWriteResponseFilter.java @@ -49,20 +49,22 @@ public class WebClientWriteResponseFilter implements GlobalFilter, Ordered { public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { // NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_ATTR is not added // until the WebHandler is run - return chain.filter(exchange) - .doOnError(throwable -> cleanup(exchange)) + return chain.filter(exchange).doOnError(throwable -> cleanup(exchange)) .then(Mono.defer(() -> { - ClientResponse clientResponse = exchange.getAttribute(CLIENT_RESPONSE_ATTR); - if (clientResponse == null) { - return Mono.empty(); - } - log.trace("WebClientWriteResponseFilter start"); - ServerHttpResponse response = exchange.getResponse(); + ClientResponse clientResponse = exchange + .getAttribute(CLIENT_RESPONSE_ATTR); + if (clientResponse == null) { + return Mono.empty(); + } + log.trace("WebClientWriteResponseFilter start"); + ServerHttpResponse response = exchange.getResponse(); - return response.writeWith(clientResponse.body(BodyExtractors.toDataBuffers())) - //.log("webClient response") - .doOnCancel(() -> cleanup(exchange)); - })); + return response + .writeWith( + clientResponse.body(BodyExtractors.toDataBuffers())) + // .log("webClient response") + .doOnCancel(() -> cleanup(exchange)); + })); } private void cleanup(ServerWebExchange exchange) { @@ -71,4 +73,5 @@ public class WebClientWriteResponseFilter implements GlobalFilter, Ordered { clientResponse.bodyToMono(Void.class).subscribe(); } } + } diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java index dbecfd35..3a72b1aa 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java @@ -247,6 +247,29 @@ public class GatewayFilterSpec extends UriSpec { .setContentType(newContentType))); } + /** + * A filter that can be used to modify the request body. This filter is BETA and may + * be subject to change in a future release. + * @param configConsumer request spec for response modification + * @return a {@link GatewayFilterSpec} that can be used to apply additional filters + *
+	 * {@code
+	 * ...
+	 * .modifyRequestBody(c -> c
+	 *		.setInClass(Some.class)
+	 *		.setOutClass(SomeOther.class)
+	 *		.setInHints(hintsIn)
+	 *		.setOutHints(hintsOut)
+	 *		.setRewriteFunction(rewriteFunction))
+	 * }
+	 * 
+ */ + public GatewayFilterSpec modifyRequestBody( + Consumer configConsumer) { + return filter(getBean(ModifyRequestBodyGatewayFilterFactory.class) + .apply(configConsumer)); + } + /** * A filter that can be used to modify the response body This filter is BETA and may * be subject to change in a future release. @@ -287,6 +310,28 @@ public class GatewayFilterSpec extends UriSpec { .setNewContentType(newContentType))); } + /** + * A filter that can be used to modify the response body using custom spec. This + * filter is BETA and may be subject to change in a future release. + * @param configConsumer response spec for response modification + * @return a {@link GatewayFilterSpec} that can be used to apply additional filters + *
+	 * {@code
+	 * ...
+	 * .modifyResponseBody(c -> c
+	 *		.setInClass(Some.class)
+	 *		.setOutClass(SomeOther.class)
+	 *		.setOutHints(hintsOut)
+	 *		.setRewriteFunction(rewriteFunction))
+	 * }
+	 * 
+ */ + public GatewayFilterSpec modifyResponseBody( + Consumer configConsumer) { + return filter(getBean(ModifyResponseBodyGatewayFilterFactory.class) + .apply(configConsumer)); + } + /** * A filter that can be used to add a prefix to the path of a request before it is * routed by the Gateway.