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
