Files
spring-cloud-gateway/docs/modules/ROOT/pages/spring-cloud-gateway-server-webflux/fluent-java-routes-api.adoc
Spencer Gibb 037abaeb26 New module names (#3645)
* New naming convention using webflux and webmvc

The new suffixes are gateway-server-web{flux|mvc} and gateway-proxyexchange-web{flux|mvc}.

This commit updates the starters to use gateway-server-web{flux|mvc} suffixes.

* Adds new spring-cloud-gateway-server-web{flux|mvc} modules.

These simply depend on the old ones. A warning is logged if not using the new module.

* Adds s-c-g-proxyexchange-{webflux|webmvc} modules

* Updates docs for  s-c-g-{proxyexchange|server}-{webflux|webmvc} modules
2025-01-16 21:26:10 -05:00

42 lines
1.5 KiB
Plaintext

[[fluent-java-routes-api]]
= Fluent Java Routes API
To allow for simple configuration in Java, the `RouteLocatorBuilder` bean includes a fluent API.
The following listing shows how it works:
.GatewaySampleApplication.java
[source,java]
----
// static imports from GatewayFilters and RoutePredicates
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) {
return builder.routes()
.route(r -> r.host("**.abc.org").and().path("/image/png")
.filters(f ->
f.addResponseHeader("X-TestHeader", "foobar"))
.uri("http://httpbin.org:80")
)
.route(r -> r.path("/image/webp")
.filters(f ->
f.addResponseHeader("X-AnotherHeader", "baz"))
.uri("http://httpbin.org:80")
.metadata("key", "value")
)
.route(r -> r.order(-1)
.host("**.throttle.org").and().path("/get")
.filters(f -> f.filter(throttle.apply(1,
1,
10,
TimeUnit.SECONDS)))
.uri("http://httpbin.org:80")
.metadata("key", "value")
)
.build();
}
----
This style also allows for more custom predicate assertions.
The predicates defined by `RouteDefinitionLocator` beans are combined using logical `and`.
By using the fluent Java API, you can use the `and()`, `or()`, and `negate()` operators on the `Predicate` class.