EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Webflux
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot Webflux

Spring Boot Webflux

Introduction to Spring Boot Webflux

Spring Boot WebFlux is a parallel of Spring MVC Version that supports a full non-blocking reactive stream. Spring Boot WebFlux uses the project reactor as the reactive library, where Reactor is the Reactive Streams Library, and hence all the operators support non-blocking back pressure which is developed in close collaboration with Spring. The reactive system needs developers and tools specially trained in implementing unique programming architectures. To get started with Spring Boot WebFlux, there are a few more concepts we need to look into. Let us look into those concepts at first and then get into WebFlux.

Getting Started with Spring Boot WebFlux

Reactive System:

Here, the term “reactive” refers to the programming models built around reacting to changes.

  • It supports asynchronous, and is event-driven, and provides a non-blocking approach to data processing. Also organizes data and events as streams.
  • In the Reactive system, when the user makes a request, other tasks will be executed, waiting for results.
  • When data is made available, notification is sent along with results via a callback function.
  • Hence the Reactive system is suitable for data-driven applications.
  • A reactive architectural pattern in a reactive system prioritizes the usage of loosely coupled, scalable, and flexible components.
  • In non-blocking code, back pressure, i.e., rate of events, needs to be controlled such that fast producer will not overwhelm the destination.
  • Reactive programming is good for applications that have streaming data, clients that consume and stream to users.
  • Reactive systems generally focus on Reactiveness, Resilience, Elasticity, and Message Driven communication.

Spring Boot WebFlux features:

Some of the features of WebFlux are listed below,

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • WebClient: It is WebFlux’s reactive web client that is built from a well-known Rest Template. It’s an interface representing the main entry point for web requests and supports both asynchronous and synchronous operations. WebClient can be built and created by importing the standard WebFlux dependencies. It is mainly used for reactive backend communication.

Maven Dependency:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

WebClient Instance:

WebClient client = WebClient.create();

  • Router functions: It is a functional alternative to Request mapping and the Controller annotation used in Spring MVC. The user uses the route() for creating routes instead of writing the complete router function. These router functions are registered as Spring beans and can be created in any configuration classes. Router functions also avoid side effects that are caused by the multi-step process for request mapping and streamline to direct handler/ router chain; this allows for functional programming.

Annotations:

RequestMapping
Controller
RouterFunctions

  • Servers: Spring Boot WebFlux is supported on Jetty, Tomcat, Servlet, and non-Servlet containers like Netty, Undertow. Users can easily switch between asynchronous and non-blocking designs with a simple change in Gradle or Maven build software.
  • Reactive Steam API: It is a collection of imported functions that allow smart stream data flow and has built-in support for asynchronous processing and back pressure, which ensures application making most efficient use for both computer and component resources. 4 major interfaces in Steam API are: Publisher, Subscriber, Subscription, and Processor
  • Concurrency Model: Spring Boot WebFlux uses a different concurrent programming model from Spring and assumes threads will be blocked by using a large thread pool at the instance of blocking. The large thread pool makes MVC resource-intensive as hardware keeps threads spun at once. Hence WebFlux uses a small thread pool as it assumes users never need to pass off the work avoiding blockers, and are called as Event Loop Workers.
  • Spring WebFlux Security: It uses Spring Security for implementing authentication and authorization protocols. It uses Web Filter to check requests against an authenticated user list or can be sent to refuse requests automatically that fit origin or request type criteria. It is a minimal implementation to set all settings to default.

Spring Boot WebFlux Example:

Different examples are mentioned below:

Step 1: Include Maven Dependencies required for Spring Boot WebFlux application.

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath />
</parent>
<groupId>com.sample</groupId>
<artifactId>spring-webflux-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-webflux-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

The main dependencies that are required to be added are spring-boot-starter-webflux, spring-boot-starter-data-mongodb-reactive, and spring-boot-starter-test and reactor-test dependencies.

Step 2: Below is the Project Structure,

Spring Boot Webflux output 1

Create the above files to have a sample WebFlux example.

Step 3: In Application.java

package com.journaldev.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Step 4: In HelloWorldWebClient.java

package com.journaldev.spring.client;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
public class HelloWorldWebClient {
public static void main(String args[]) {
WebClient client = WebClient.create("https://localhost:8080");
Mono<ClientResponse> result = client.get().uri("/helloWorld").accept(MediaType.TEXT_PLAIN).exchange();
System.out.println("Result = " + result.flatMap(res -> res.bodyToMono(String.class)).block());
}
}

Step 5: In HelloWorldHandler.java

package com.journaldev.spring.component;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
@Component
public class HelloWorldHandler {
public Mono<ServerResponse> helloWorld(ServerRequest request) {
return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromObject("Hello World!"));
}
}

Step 6: In HelloWorldRouter.java

package com.journaldev.spring.component;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Configuration
public class HelloWorldRouter {
@Bean
public RouterFunction<ServerResponse> routeHelloWorld(HelloWorldHandler helloWorldHandler) {
return RouterFunctions.route(RequestPredicates.GET("/helloWorld")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), helloWorldHandler::helloWorld);
}
}

On Running the Application,

Console Output:

Spring Boot Webflux output 2

Conclusion

With this, we shall conclude the topic “Spring Boot WebFlux.” We have seen what is Spring boot webflux and what it means in Java Spring. We have also seen how WebFlux works along with features. Had a clear overview of “Reactive System,” Based on the example shown above, have displayed a small text message, based on the requirement, functionalities can be added. I hope this helps. Thanks! Happy Learning!!

 Recommended Articles

This is a guide to Spring Boot Webflux. Here we discuss what is webflux and what it means in Java Spring and also see how WebFlux works along with features. You may also have a look at the following articles to learn more –

  1. Spring Boot Initializr
  2. Spring Boot Cache
  3. Spring Boot Docker
  4. Spring Boot HTTPS
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more