Skip to content

Commit 6100cc4

Browse files
pachatspencergibb
authored andcommitted
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 <pacifiquepachat.hategekimana@risa.gov.rw>
1 parent 518f4d4 commit 6100cc4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/modules/ROOT/pages/spring-cloud-gateway-server-webflux/request-predicates-factories.adoc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,40 @@ spring:
376376

377377
This route matches if the `X-Forwarded-For` header contains, for example, `192.168.1.10`.
378378

379+
380+
[[read-body-route-predicate-factory]]
381+
== The ReadBody Route Predicate Factory
382+
383+
The `ReadBody` route predicate factory reads the request body and applies a user-provided
384+
predicate against it. The body is cached in memory after the first read, so subsequent
385+
evaluations do not need to deserialize it again.
386+
387+
NOTE: This predicate can only be configured programmatically via the Java DSL.
388+
It cannot be configured via YAML because the `predicate` parameter requires a Java
389+
`Predicate` lambda, which cannot be expressed in a configuration file.
390+
391+
The following example configures a ReadBody route predicate:
392+
393+
.GatewayConfig.java
394+
[source,java]
395+
----
396+
@Bean
397+
public RouteLocator routes(RouteLocatorBuilder builder) {
398+
return builder.routes()
399+
.route("read-body-route", r -> r
400+
.readBody(String.class, body -> body.contains("yes"))
401+
.uri("https://example.org"))
402+
.build();
403+
}
404+
----
405+
406+
In the preceding example, the predicate checks whether the request body (deserialized as
407+
a `String`) contains the word `yes`. If so, the route matches and the request is forwarded
408+
to `https://example.org`.
409+
410+
The `readBody` method accepts two parameters:
411+
412+
* `inClass` - the class to deserialize the request body into (for example, `String.class`
413+
or a custom request DTO class).
414+
* `predicate` - a Java `Predicate<T>` that receives the deserialized body and returns
415+
`true` if the route should match.

0 commit comments

Comments
 (0)