SpringBoot WebFlux Functional 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 Functional restful APIs.

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

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 Functional

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

With Spring WebFlux Functional, we use {HandlerFunctions, RouterFunctions} to develop.

1. HandlerFunctions

HandlerFunctions will handle incoming HTTP requests through ServerRequest, and return a Mono

2. RouterFunction

RouterFunction handle all incoming requests. It takes a ServerRequest, and returns a Mono. If a request matches a particular route, a handler function is returned; otherwise it returns an empty Mono.

III. Practice

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

springboot webflux functional - project structure

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

1. Create SpringBoot project

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

spring-webflux-reactive-select-reactive-web

Check pom.xml after creating:

2. Create data model

Create a Customer data model:

3. Implement repository
3.1 Define interface CustomerRepository

3.2 Implement CustomerRepository

4. Implement Spring WebFlux APIs
4.1 RouterFunction

4.2 CustomerHandler

5. Run and check results

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

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

springboot webflux functional - get all customer

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

springboot webflux functional - get a customer by id

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

springboot webflux functional - post a customer

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

springboot webflux functional - put a customer

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

springboot webflux functional - delete a customer

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

springboot webflux functional - final get all customer to check

IV. Sourcecode

SpringWebFluxFunctional

By JavaSampleApproach | September 27, 2017.


Related Posts


Got Something To Say:

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

*