Notice how we made use of the main building blocks of this API:
Route – the primary API of the gateway. It is defined by a given identification (ID), a destination (URI) and set of predicates and filters
Predicate – a Java 8’s Predicate – which is used for matching HTTP requests using headers, methods or parameters
Filter – a standard Spring’s WebFilter
3. Dynamic Routing
Just like Zuul, Spring Cloud Gateway provides means for routing requests to different services.
The routing configuration can be created by using pure Java (RouteLocator, as shown in the example in section 2.1) or by using properties configuration:
- id: baeldung
- id: myOtherRouting
4. Routing Factories
Spring Cloud Gateway matches routes using the Spring WebFlux HandlerMapping infrastructure.
It also includes many built-in Route Predicate Factories. All of these predicates match different attributes of the HTTP request. Multiple Route Predicate Factories can be combined via the logical “and”.
Route matching can be applied both programmatically or via configuration properties file using a different type of Route Predicate Factories.
4.1. Before Route Predicate Factory
The Before Route Predicate Factory takes one parameter: a datetime. This predicate matches requests that happen before the current datetime:
The Between Route Predicate Factory takes two parameters: datetime1, and datetime2. This predicate matches requests that happen after datetime1(inclusive) and before datetime2 (exclusive). The datetime2 parameter must be after datetime1: