liveBook · Manning

Requestpredicate

As explained in Spring in Action, Sixth Edition

A Spring functional web API utility class providing predicates for matching HTTP requests.

A RequestPredicate declares which HTTP requests a functional WebFlux API should handle. RequestPredicates provides helpers such as GET(), while RouterFunction connects the predicate to handler code without annotations. section 12.2

A functional request route brings together four roles:

  • RequestPredicate selects the kind of request to handle. section 12.2
  • RouterFunction maps matching requests to handler code. section 12.2
  • ServerRequest represents the incoming HTTP request. section 12.2
  • ServerResponse represents the returned HTTP response. section 12.2

Match GET /hello

route(GET("/hello"), request -> ok().body(just("Hello World!"), String.class)) creates a predicate for GET requests to /hello and sends a reactive Hello World! response through the matching handler. section 12.2

package hello;
 
import static org.springframework.web
                 .reactive.function.server.RequestPredicates.GET;
import static org.springframework.web
                 .reactive.function.server.RouterFunctions.route;
import static org.springframework.web
                 .reactive.function.server.ServerResponse.ok;
import static reactor.core.publisher.Mono.just;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
 
@Configuration
public class RouterFunctionConfig {
 
  @Bean
  public RouterFunction<?> helloRouterFunction() {
    return route(GET("/hello"),
        request -> ok().body(just("Hello World!"), String.class))
      ;
  }
 
}

View a complete configuration class that declares the route as a Spring bean and uses static helper imports. section 12.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains request selection and demonstrates a GET path predicate inside a working route.
Examples
Uses GET("/hello") to return a reactive Hello World! response.
What you can do after
Apply Spring WebFlux functional routing by matching requests and attaching handler lambdas.
How it connects
Connects RequestPredicate with RouterFunction, ServerRequest, ServerResponse, and WebFlux.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.