@@ -448,3 +448,40 @@ spring:
448448
449449This route matches if the `X-Forwarded-For` header contains, for example, `192.168.1.10`.
450450
451+
452+ [[read-body-route-predicate-factory]]
453+ == The ReadBody Route Predicate Factory
454+
455+ The `ReadBody` route predicate factory reads the request body and applies a user-provided
456+ predicate against it. The body is cached in memory after the first read, so subsequent
457+ evaluations do not need to deserialize it again.
458+
459+ NOTE: This predicate can only be configured programmatically via the Java DSL.
460+ It cannot be configured via YAML because the `predicate` parameter requires a Java
461+ `Predicate` lambda, which cannot be expressed in a configuration file.
462+
463+ The following example configures a ReadBody route predicate:
464+
465+ .GatewayConfig.java
466+ [source,java]
467+ ----
468+ @Bean
469+ public RouteLocator routes(RouteLocatorBuilder builder) {
470+ return builder.routes()
471+ .route("read-body-route", r -> r
472+ .readBody(String.class, body -> body.contains("yes"))
473+ .uri("https://example.org"))
474+ .build();
475+ }
476+ ----
477+
478+ In the preceding example, the predicate checks whether the request body (deserialized as
479+ a `String`) contains the word `yes`. If so, the route matches and the request is forwarded
480+ to `https://example.org`.
481+
482+ The `readBody` method accepts two parameters:
483+
484+ * `inClass` - the class to deserialize the request body into (for example, `String.class`
485+ or a custom request DTO class).
486+ * `predicate` - a Java `Predicate<T>` that receives the deserialized body and returns
487+ `true` if the route should match.
0 commit comments