SpringBoot WebFlux Annotation-based RestAPIs
Tags: springboot, webflux, restapi, annotation
Published: 2021-05-03 06:41:01

https://grokonez.com/spring-framework/spring-webflux/springboot-webflux-annotation-based-programming-model

SpringBoot WebFlux Annotation-based 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</code> module, 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 Annotation-based</strong> restful APIs.

Related posts:
- <a href="https://grokonez.com/spring-framework/spring-boot/springboot-webflux-functional-restapis">SpringBoot WebFlux Functional 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>
- <a href="https://grokonez.com/spring-framework/spring-boot/springboot-webflux-test">SpringBoot WebFlux Test</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</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>Annotation-based</strong>.
For starting with WebFlux, SpringBoot supports a collection dependency: <code>spring-boot-starter-webflux</code>.

Sample code:

<pre><code class="language-java">
@RestController
@RequestMapping(value="/api/customer")
public class RestControllerAPIs {
	
    @GetMapping("/")
    public Flux<Customer> getAll() {
	
    	...
    }
    
	@GetMapping("/{id}")
    public Mono<Customer> getCustomer(@PathVariable Long id) {
		
		...
    }
}
</code></pre>

- <code>reactor.core.publisher.Flux</code>: is a standard <code>Publisher<T></code> representing a reactive sequence of <strong>0..N</strong> items, optionally terminated by either a success signal or an error.
- <code>reactor.core.publisher.Mono</code>: <code>Mono<T></code> is a specialized <code>Publisher<T></code> that emits at most single-valued-or-empty result.
<h2>III. Practice</h2>
In the tutorial, We create a SpringBoot project as below:

<img src="https://grokonez.com/wp-content/uploads/2017/09/springboot-webflux-annotation-based-project-structure-.png" alt="springboot webflux annotation-based - project structure" width="370" height="181" class="alignnone size-full wp-image-8167" />

<strong>Step to do</strong>:
- Create SpringBoot project
- Create data model
- Implement Spring WebFlux APIs
- Run and check results
<h3>1. Create SpringBoot project</h3>
Using <strong>SpringToolSuite</strong>, create a SpringBoot project with <code>Reactive Web</code> dependency:

<img src="https://grokonez.com/wp-content/uploads/2017/09/spring-webflux-reactive-select-reactive-web.png" alt="springboot webflux reactive - select reactive web" width="391" height="215" class="alignnone size-full wp-image-8168" />

Check <code>pom.xml</code> after creating:

More at:

https://grokonez.com/spring-framework/spring-webflux/springboot-webflux-annotation-based-programming-model

SpringBoot WebFlux Annotation-based RestAPIs