Search code examples
javaspring-bootpredicatespring-cloud-gateway

Spring Cloud Gateway route builder has missing predicate option


I want to add a predicate to my spring gateway route below

public RouteLocator routeLocator(RouteLocatorBuilder builder,
                                     CredentialsAuthRoutePredicateFactory ca) {
        return builder.routes()
                .route("superGraph - get data",r -> r
                        .path( "/alcoholic-alpaca/**")
                        .predicate(...)

However, the predicate option is underlined, and this error is showing up:

'predicate' is not public in 'org.springframework.cloud.gateway.route.builder.BooleanSpec'. Cannot be accessed from outside package

How can i access predicate in this case?


Solution

  • path(String path) method of PredicateSpec is also a predicate (like a special case), so if you want multiple predicates you should call BooleanSpec's and() or or() method (depends on your scenario) and then call another predicate method, like this:

    public RouteLocator routeLocator(RouteLocatorBuilder builder, CredentialsAuthRoutePredicateFactory ca) {
        return builder.routes()
                .route("superGraph - get data",
                        r -> r.path( "/alcoholic-alpaca/**")
                            .and() // or() ?
                            .predicate(alpacaPredicate)
                            .filters(f -> f.filter(alpacaFilter))
                            .uri(soberAlpacaUri))
                .build();
    }