Functional web framework

Building on top of the reactive features, Spring 5 also provides a functional web framework.

A functional web framework provides features to define endpoints using functional programming style. A simple hello world example is shown here:

    RouterFunction<String> route =    route(GET("/hello-world"),    request -> Response.ok().body(fromObject("Hello World")));

A functional web framework can also be used to define more complex routes, as shown in the following example:

    RouterFunction<?> route = route(GET("/todos/{id}"),    request -> {       Mono<Todo> todo = Mono.justOrEmpty(request.pathVariable("id"))       .map(Integer::valueOf)       .then(repository::getTodo);       return Response.ok().body(fromPublisher(todo, Todo.class));      }) .and(route(GET("/todos"), ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.