SpringBoot WebFlux Functional RestAPIs
Tags: springboot, webflux, functional, restapis
Published: 2021-05-03 06:48:35

https://grokonez.com/spring-framework/spring-webflux/springboot-webflux-functional-restapis

SpringBoot WebFlux Functional RestAPIs

<a href="https://grokonez.com/category/reactive-programming">Reactive programming</a> is about non-blocking applications. And <strong>Spring Framework 5</strong> includes a new <code>spring-webflux module</code>, supports <strong>Reactive Streams</strong> for communicating backpressure across async components and libraries. So in the tutorial, <a href="https://grokonez.com">JavaSampleApproach</a> will guide you through the steps for creating a <strong>SpringBoot WebFlux Functional</strong> restful APIs.

Related posts:
- <a href="https://grokonez.com/spring-framework/spring-webflux/springboot-webflux-annotation-based-programming-model">SpringBoot WebFlux Annotation-based RestAPIs</a>
– <a href="https://grokonez.com/reactive-programming/reactor/reactor-create-flux-and-mono-simple-ways-to-create-publishers-reactive-programming">Reactor – Simple Ways to create Flux/Mono</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/spring-webclient-spring-webflux-springboot-2">Spring WebClient with Spring Webflux | SpringBoot 2</a>

<!--more-->
<h2>I. Technologies</h2>
– Java: 1.8
– Maven: 3.3.9
– Spring Tool Suite: Version 3.9.0.RELEASE
– Spring Boot: 2.0.0.M4
– Spring Boot Starter Webflux
<h2>II. Spring WebFlux Functional</h2>
<strong>Spring Framework 5.0</strong> supports WebFlux with fully asynchronous and non-blocking and does NOT require the Servlet API(Unlike Spring MVC).

Spring WebFlux supports 2 distinct programming models:
– Annotation-based with @Controller
– Functional with Java 8 lambda style

In the tutorial, we will introduce WebFlux with <strong>Functional</strong>.
For starting with WebFlux, SpringBoot supports a collection dependency: <code>spring-boot-starter-webflux</code>.

With <strong>Spring WebFlux Functional</strong>, we use {<code>HandlerFunctions</code>, <code>RouterFunctions</code>} to develop.
<h3>1. HandlerFunctions</h3>
<strong>HandlerFunctions</strong> will handle incoming HTTP requests through <code>ServerRequest</code>, and return a <code>Mono<ServerResponse></code>

<pre><code class="language-java">
@Component
public class CustomerHandler {
	
	...
	
    public Mono<ServerResponse> getAll(ServerRequest request) {
		...
		return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(customers, Customer.class);
    }
    
    ...
    
    public Mono<ServerResponse> putCustomer(ServerRequest request) {
		...
		return responseMono
                .flatMap(cust -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(fromObject(cust)));
    }

    ...
	
    public Mono<ServerResponse> deleteCustomer(ServerRequest request) {
		...
		return responseMono
                .flatMap(strMono -> ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(fromObject(strMono)));
    }
}</code></pre>

<h3>2. RouterFunction</h3>
<strong>RouterFunction</strong> handle all incoming requests. It takes a <code>ServerRequest</code>, and returns a <code>Mono<HandlerFunction></code>. If a request matches a particular route, a handler function is returned; otherwise it returns an <strong>empty Mono</strong>.

More  at:

https://grokonez.com/spring-framework/spring-webflux/springboot-webflux-functional-restapis

SpringBoot WebFlux Functional RestAPIs