Join Christopher Anatalio for an in-depth discussion in this video Spring WebFlux overview, part of Reactive Spring.
- [Instructor] Okay, now let's dive into the Spring WebFlux framework. Let's take a look at the stack. First of all, we have the actual Spring WebFlux library, which you could think of as a reactive version of Spring MVC. Next, we have the Spring Web API, which is similar in functionality to the Servlet API and is built upon Project Reactor. The Spring Web API interacts with the web server through non-blocking I/O.
Any Servlet container that supports the Servlet 3.1 non-blocking I/O API can be used. You can also use Frameworks or web servers such as Netty or Undertow. WebFlux helps you create reactive web applications. You will have all the tools you need to make reactive REST calls to a remote service, and it contains everything you need to implement a reactive server web application with REST, HTML, or even WebSockets.
There are two primary programming models in WebFlux. First of all, we have annotation style using @Controller and other familiar Spring MVC-style annotations. We also have the functional style, which uses Java 8 lambdas for routing and handling. Delving a bit deeper into the annotation model, we can see that WebFlux supports all the same annotations as Spring MVC such as Request Mapping, Get Mapping, and Post Mapping.
However, we will return a Mono or a Flux instead of a ResponseEntity. If you expect at most one result, return a Mono. If a list is expected, return a Flux. Looking into the internals WebFlux, we could see that each runtime is adapted to a reactive ServerHttpRequest and Response while the request body is exposed as a Flux of the type DataBuffer rather than a InputStream or an Output Stream.
WebFlux needs a web server to actually serve requests. Spring Boot helps a lot in this regard. It has a WebFlux starter dependency that automates configuration of your web server. By default the web server is Netty. However, you can easily switch to Tomcat, Jetty, Undertow, or any other web server by updating your Gradle dependencies. Why Netty? It is widely used in the async, non-blocking space and is considered a first mover as per the Spring team.
- Project Reactor and Reactive Core
- Spring WebFlux
- Reactive Spring Data with MongoDB
- Bootstrapping a reactive web app with Spring Boot 2
- Writing reactive Spring controllers
- Working with Spring reactive data
- Spring reactive unit testing
- Writing tests using the WebTestClient