SpringBoot WebFlux Annotation-based RestAPIs

Reactive programming is about non-blocking applications. And Spring Framework 5 includes a new spring-webflux module, supports Reactive Streams for communicating backpressure across async components and libraries. So in the tutorial, JavaSampleApproach will guide you through the steps for creating a SpringBoot WebFlux Annotation-based restful APIs.

Related posts:
SpringBoot WebFlux Functional RestAPIs
Reactor – Simple Ways to create Flux/Mono
Spring WebClient with Spring Webflux | SpringBoot 2
SpringBoot WebFlux Test

I. Technologies

– 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

II. Spring WebFlux

Spring Framework 5.0 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 Annotation-based.
For starting with WebFlux, SpringBoot supports a collection dependency: spring-boot-starter-webflux.

Sample code:

reactor.core.publisher.Flux: is a standard Publisher representing a reactive sequence of 0..N items, optionally terminated by either a success signal or an error.
reactor.core.publisher.Mono: Mono is a specialized Publisher that emits at most single-valued-or-empty result.

III. Practice

In the tutorial, We create a SpringBoot project as below:

springboot webflux annotation-based - project structure

Step to do:
– Create SpringBoot project
– Create data model
– Implement Spring WebFlux APIs
– Run and check results

1. Create SpringBoot project

Using SpringToolSuite, create a SpringBoot project with Reactive Web dependency:

springboot webflux reactive - select reactive web

Check pom.xml after creating:

2. Create data model

– Create a Customer model class:

3. Implement Spring WebFlux APIs

Implement WebFlux APIs:

4. Run and check results

Build and run the SpringBoot project with commandlines: {mvn clean install, mvn spring-boot:run}

– See console’ logs:

– Make a GET all customer request: http://localhost:8080/api/customer/

springboot webflux reactive - get all request

– Make a GET customer request: http://localhost:8080/api/customer/1/

springboot webflux reactive - get a customer

– Make a POST request: http://localhost:8080/api/customer/post

springboot webflux reactive - post a customer

– Make a PUT request: http://localhost:8080/api/customer/put/3

springboot webflux reactive - put a customer

– Make a DELETE request: http://localhost:8080/api/customer/delete/1

springboot webflux reactive - delete request

– Make a GET all customers request: http://localhost:8080/api/customer/

springboot webflux reactive - get all customer for final check

IV. Sourcecode

SpringBootWebFluxAnnotationBased


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*