Using predicates in Spring route definition does not work

511 views Asked by At

I am trying to define a Route using Spring's WebFlux modules. Here is my route defintion:

@Bean
public RouterFunction<?> routes(Handler handler) {
    Predicate<ServerRequest.Headers> predicate = headers -> headers.equals("clientId");
    return
            route(GET("/api/v1/client/info").and(headers(predicate)),handler::getInfo);
}

My intent here is to define an GET endPoint with a certain path and the client must provide request header called "clientId". This definition does not work when I make the call to the endPoint. However, if I take the header() part out, the call goes through. What am I missing here ? Kindly advise.

Thank you.

1

There are 1 answers

0
TomerBu On

As said in the comments: You can't compare a String to a header.
The Predicate needs to check that headers.firstHeader("clientId") is not null:

Predicate<ServerRequest.Headers> clientIdPredicate =
                headers -> headers.firstHeader("clientId") != null;