Introduction to Spring WebFlux

Man working at night coding and pointing on a screen with a pen
Reading Time: 3 minutes

Spring 5 is the first Spring framework that offers built-in support for reactive programming. This blog is an introduction to Spring WebFlux’s . Spring frameworks that have built-in support for reactive programming. so first we will understand what is reactive programming.

Reactive programming

It is a programming paradigm that used an asynchronous, non-blocking, event-driven approach to data processing. Reactive programming added modeling data and events to observe data streams and implemented data processing routines to react to the changes in those streams.

What is Spring WebFlux ?

It is a non-blocking, annotation-based web framework. WebFlux uses Project Reactor and its publisher implementations, Flux, and Mono. WebFlux uses a router functions feature to apply functional programming to the web layer and bypass declarative controllers and Request Mapping.

It supports two programming models:

  • Annotation-based reactive components
  • Functional routing and handling

Dependencies

Let’s start with the spring-boot-starter-webflux dependency

  • spring-boot and spring-boot-starter for basic Spring Boot application setup
  • spring-webflux framework
  • reactor-core that we need for reactive streams and also reactor-netty
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <version>2.6.4</version>
  </dependency>

Spring Boot WebFlux features:

  • WebClient: It is an interface representing the main entry point for performing web requests. it offers support for both synchronous and asynchronous operations. WebClient can be built and created by importing the standard WebFlux dependencies. It is mainly used for backend communication.

maven Dependency:

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

Gradle dependency:

     dependencies {
      compile 'org.springframework.boot:spring-boot-starter-webflux'
     }

    WebClient Instance:

    WebClient client = WebClient.create();
  • Router functions: It serves as an alternative to the @RequestMapping annotation and the @Controller annotation used in Spring MVC. The user uses the RouterFunctions.route() to create routes, instead of writing a complete router function and route requests to the handler functions.
      @FunctionalInterface
          public interface RouterFunction<T extends ServerResponse> {
          Mono<HandlerFunction<T>> route(ServerRequest request);
        }
  • Servers: Spring Boot WebFlux is supported on Jetty, Tomcat, Servlet, and non-Servlet containers like Netty, and Undertow. Users can easily switch between asynchronous and non-blocking designs with a simple change in Gradle or Maven build software.

  • Reactive Stream API: Reactive Streams has built-in support for asynchronous processing with non-blocking back pressure, which ensures the application makes the most efficient use of both computer and component resources. 4 major interfaces in Stream APIs are Publisher, Subscriber, Subscription, and Processor.

  • Concurrency Model: It uses concurrent programming and It 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. WebFlux uses a small thread pool as it assumes users never need to pass off the work avoiding blockers and are called Event Loop Workers.

  • Spring WebFlux Security: It uses Spring WebFlux Security for implementing authentication and authorization. The class must be annotated with @EnableWebFluxSecurity to enable flux security for a web app. It uses Web Filter to filter out the request and authenticated user list.

Conclusion:

In this blog, we explored reactive web components as supported by the Spring WebFlux framework and we also covered the dependency which we have to use in spring WebFlux and we covered various features of Spring WebFlux.

must read: https://blog.knoldus.com/introduction-of-spring-cloud-gateway/

Written by 

I'm a Software Consultant at Knoldus Inc. I have done Post Graduation from Quantum University Roorkee. I have knowledge of various programming languages. I'm passionate about Java development and curious to learn Java Technologies. I'm always ready to learn new technologies and my hobbies are cricket and action movies.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading