Spring Boot Fundamentals

Reactiver Programming with Spring Boot

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment covers reactive programming, project reactor, the Spring WebFlux module and how we can create reactive apps with Spring Boot.

Keywords

  • spring webflux
  • reactive programming
  • reactor
  • reactive
  • observer
  • spring boot
  • javarx

About this video

Author(s)
Felipe Gutierrez
First online
22 May 2021
DOI
https://doi.org/10.1007/978-1-4842-7066-0_17
Online ISBN
978-1-4842-7066-0
Publisher
Apress
Copyright information
© Felipe Gutierrez 2021

Video Transcript

Hello and welcome back. Let’s start with Reactive Programming with Spring Boot. But what is Reactive Programming?

Reactive Programming is all about data streams and propagation of change. Take a look. We can use Reactive Programming in spreadsheets, like event-driven architectures, of course, high concurrent messaging, doing synchronous or asynchronous messaging or async processing.

But if you think a little bit, you can see that Reactive Programming is like observing design pattern So imagine this. That’s where you have an observer, you have several observers, you have a subject that is changing state, and automatically will notify to the other observers about that change. So this is actually very similar on what Reactive Programming is.

For that, the Spring Team created the Project Reactor. The Project Reactor is an implementation of the Reactive Programming part of that. Reactor is going to offer the non-blocking, backpressure-ready embeddable solutions. And normally, that will include local or remote unicast or multicast messaging through TCP, HTTP, UDP.

Reactor offers two particular APIs– Flux that can deal with several elements, or Mono, with zero or one. So that’s part of implementing reactive extensions.

So we see Mono. We can see it on a timeline, that we can have from zero or one particular element that actually can apply certain kind of operators over this particular element. And you can have that result, again, over time.

If we see Flux, we can have multiple guys, again, apply certain kind of operators. And at some point, practice this kind of a back compression, meaning that you can tell your producer, hey, stop sending me more stuff or, while you are applying, send me the next. So that will be how we can see Flux as a way to execute several elements and apply certain kinds of operators.

We can see the whole Project Reactor like this. Normally, we are going to have a core. Practically, a Project Reactor is talking about Stream’s IO, definitions, and Addons. And of course, everything is based on the Spring, kind of a base programming.

Now with this Spring Framework, and in this case, the Spring Framework 5– created the Reactive Streams– embraces this particular concept by creating this new module named spring-webflux. With this particular module, the Spring Framework will support Reactive HTTP, WebSocket clients, Reactive Servers. And, of course, will expose the same elements, the same API, from Project Reactor, the Flux and Mono.

On the server-side, WebFlux is going to support, two different programming models. One will be annotation-based, something that you are familiar with, and then the other will be functional, very similar to Java 8 lambda style.

On the client-side, WebFlux will include a reactive WebClient that is an alternative to the RestTemplate, that you also already know. And of course, it will support the WebSockets and have the same WebTestClient for testing support.

Now, let’s take a look into the Spring WebFlux based on the annotation. So you are already familiar with this. So take a look. Practically, we are using, here, in this example, the same RestController, the RequestMapping, the GetMapping. But the difference here is that now, the return time is different. In this case, it could be either a Mono or a Flux. So that’s the way that we can use annotation based.

If we go into the functional, take a look that right now, we have a configuration we declare as being, where normally we say, hey, these are going to be my RouterFunctions that you’re going to use. So in this case, take a look that we are going to have to implement our ServerResponse. And that ServerResponse always is going to return a MonoServerResponse. So this is the way that normally we are going to do a Spring WebFlux using functional programming.

Now on the client side, it’s very simple. Just do WebClient create and then go to the URL where you want to do. And of course, then you can execute whatever operators you want to do. In this case, for example, like the accept(APPLICATION_JSON). When you get back certain information, do part of the convert, that particular class into, for example, in this case, a Mono type of instance.

Now, we can see the bigger picture for Spring WebFlux. So take a look. Here, we have the Servlet Container, now, or the Reactive application servers like the Tomcat, Jetty, Netty, Undertow that allows us to do HTTP/ Reactive Streams. Everything will be thanks to the Spring WebFlux. And again, we have the two familiar ways to do programming– Controller, the regular annotations, or functional programming with Router Functions. And again, you can use both. You can use the spring-webmvc and Spring WebFlux. So that’s part of the new model of Spring WebFlux.

Now, what happened with the Spring Boot and Reactor? Well, the only thing, if you want to start adding WebFlux to your Spring Boot applications, the only thing that you need to have, as you already guessed, is to add the spring-boot-starter technology. And in this case, the spring-boot-starter-webflux. This will help you to auto-configure the HttpMessageReader and HttpMessageWriter so you can have the response that you remember. It could be a Flux, it could be Mono, or it could be a cyber response based on a Mono type.

Of course, WebFlux also is going to help you to do configuration for the WebFluxConfigurer. Also, Spring WebFlux, together with the Spring Boot, is going to support all these new templating technologies, including Thymeleaf, FreeMarker and Mustache. Those are the most common functional and reactive ready template engines. And of course, it’s going to help you with Error Handling, with AbstractErrorWeb ExceptionHandler, and the WebFlux functional way.

Now, this is a nice thing, this is one cool feature. When you have the spring boot starter actuator, and we’re going to see that in the next segment, but when you add these and if you have also the spring-boot-starter-webflux, automatically, Spring Boot, based on that, is going to say, hey, I have a WebFlux and I have a spring-boot-web-actuator. So every single response from the web Actuator, in this case, from the spring-boot-actuator, are going to be exposed as Mono or Flux types. And we’re going to see that in action in the next segments.

So let’s take a look into our Reactive Programming Demo with Spring Boot.