From 7c1c49acc75a582d77687ade00f45320a2732f48 Mon Sep 17 00:00:00 2001 From: pachat Date: Wed, 8 Apr 2026 16:57:52 +0200 Subject: [PATCH] docs: add ReadBody route predicate factory documentation Adds documentation for ReadBodyRoutePredicateFactory which was missing from the request predicate factories reference guide. Documents Java DSL usage and explicitly notes that YAML configuration is not supported since the predicate parameter requires a Java lambda. Fixes #3569 Signed-off-by: pachat --- .../request-predicates-factories.adoc | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/docs/modules/ROOT/pages/spring-cloud-gateway-server-webflux/request-predicates-factories.adoc b/docs/modules/ROOT/pages/spring-cloud-gateway-server-webflux/request-predicates-factories.adoc index 6a64dd4ba7..8a493cdc18 100644 --- a/docs/modules/ROOT/pages/spring-cloud-gateway-server-webflux/request-predicates-factories.adoc +++ b/docs/modules/ROOT/pages/spring-cloud-gateway-server-webflux/request-predicates-factories.adoc @@ -448,3 +448,40 @@ spring: This route matches if the `X-Forwarded-For` header contains, for example, `192.168.1.10`. + +[[read-body-route-predicate-factory]] +== The ReadBody Route Predicate Factory + +The `ReadBody` route predicate factory reads the request body and applies a user-provided +predicate against it. The body is cached in memory after the first read, so subsequent +evaluations do not need to deserialize it again. + +NOTE: This predicate can only be configured programmatically via the Java DSL. +It cannot be configured via YAML because the `predicate` parameter requires a Java +`Predicate` lambda, which cannot be expressed in a configuration file. + +The following example configures a ReadBody route predicate: + +.GatewayConfig.java +[source,java] +---- +@Bean +public RouteLocator routes(RouteLocatorBuilder builder) { + return builder.routes() + .route("read-body-route", r -> r + .readBody(String.class, body -> body.contains("yes")) + .uri("https://example.org")) + .build(); +} +---- + +In the preceding example, the predicate checks whether the request body (deserialized as +a `String`) contains the word `yes`. If so, the route matches and the request is forwarded +to `https://example.org`. + +The `readBody` method accepts two parameters: + +* `inClass` - the class to deserialize the request body into (for example, `String.class` +or a custom request DTO class). +* `predicate` - a Java `Predicate` that receives the deserialized body and returns +`true` if the route should match.