liveBook · Manning

Serverrequest

As explained in Spring in Action, Sixth Edition

A Spring functional web API type representing an incoming HTTP request, including its headers and body.

ServerRequest represents an incoming HTTP request in Spring’s functional reactive web API. It provides access to request headers and body information and is passed to handler code when a route matches. section 12.2

ServerRequest works with the other core functional API types:

  • RequestPredicate declares which requests should match. section 12.2
  • RouterFunction connects matching requests to handler code. section 12.2
  • ServerResponse represents the response returned by the handler. section 12.2

A request in a functional route

In a GET route for /hello, the handler lambda accepts a ServerRequest parameter, then returns a ServerResponse containing “Hello World!”. The request parameter can be named request even when the example does not read it directly. 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 functional route where the handler receives the request and builds an HTTP response. section 12.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines request access to headers and bodies, then uses it as a handler parameter without detailing request internals.
Examples
Uses a GET /hello route whose lambda accepts ServerRequest and returns a Hello World response.
What you can do after
Apply it via Spring WebFlux functional routing by accepting request data in handler lambdas.
How it connects
Connects RequestPredicate and RouterFunction routing with ServerResponse generation.

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.