The handler function

The handler function is responsible for serving a given request. It takes the request in the form of a ServerRequest class and generates the response as ServerResponse. Both ServerRequest and ServerResponse are immutable Java 8 classes. These classes support reactive types of Mono and Flux for reading/writing data passed in the body of the request/response. Let's try to build our first hello-world example using the preceding components:

HandlerFunction<ServerResponse> helloHandler = request -> {            Optional<String>name=request.queryParam("name");            return ServerResponse.ok().body(fromObject("Hello to " +name.orElse("the world.")));        };

In the preceding code, we are doing the following:

  • The lambda takes a ServerRequest input ...

Get Hands-On Reactive Programming with Reactor now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.