RouterFunction

Incoming requests are intercepted by RouterFunction, and, according to the configured route, it is navigated to the right HandlerFunction. If the route is matched; RouterFunction takes in ServerRequest and returns back Mono<HandlerFunction>. If not, empty Mono is returned.

RouterFunction is created as shown in the following code snippet:

RouterFunctions.route(RequestPredicate, HandlerFunction)

RequestPredicate is a utility class that has predefined matching patterns for most of the common use cases, such as matching based on path, content type, HTTP method, and so on. An example code snippet for RouterFunction is as follows:

RouterFunction<ServerResponse> routeFunctionSample =
RouterFunctions.route(RequestPredicates.path("/sample-route"),
request -> Response.ok().body(fromObject("Sample Route")));

Multiple RouterFunction objects can be composed by invoking the following method:

RouterFunction.and(RouterFunction)

There is also a convenient method, as follows, which is a combination of the RouterFunction.and() and RouterFunctions.route() methods:

RouterFunction.andRoute(RequestPredicate, HandlerFunction)

The RouterFunction for the previous HandlerFunction is as follows:

RouterFunction<ServerResponse> movieRoutes =
route(GET("/movie/{id}").and(accept(APPLICATION_JSON)), handler::getMovie)
.andRoute(GET("/movie").and(accept(APPLICATION_JSON)), handler::listMovies)
.andRoute(POST("/movie").and(contentType(APPLICATION_JSON)), handler::createMovie);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.105.186