Spring has introduced webFlux new reactive web framework to support reactive programming.
“Reactive programming is about processing an asynchronous stream of data items, where applications react to the data items as they occur. A stream of data is essentially a sequence of data items occurring over time. This model is more memory efficient because the data is processed as streams, as compared to iterating over the in-memory data” (define at Oracle docs) .
For example, in an imperative programming setting, a = b +c would mean that a is being assigned the result of b + c in the instant the expression is evaluated , and letter the values of b and/or c can be changed with no effect on the value of a, However, in reactive programming, the valud of a is automatically updated whenever the values of b and/or c changes, without the program having to re-execute the sentence a= b + c to determine the presently assigned value of a (click here for more detail) .
Spring 5 webFlux and Reactive basic(RxJava or Project Reactor)
let generate spring boot template from Spring Initializr
now import the generated project in your IDE and create Customer data class, check below example:-
@Document applied at the class level to indicate this class is a candidate for mapping to the database. You can specify the name of the collection where the database will be stored.
@Field applied at a variable level to define column name.
data class is specialized for hold data.
then created ‘Repository’ class look like below.
above class, I’ve extended ReactiveMongoReposiotry instead of MongoRepository because we are using Reactive features like Mono and Flux.
once ‘CustomerRepo’ repository classes created then create a handler for it, below code sample.
ServerRequest and ServerResponse introduce in spring 5, we can extract request param, path variable, header information and body details from ServerRequest.
Mono is a specialized Publisher that emits at most one item and then optionally terminates with an onComplete signal or an onError signal. click here for more detail.
Flux is a standard ‘Publisher’ representing an asynchronous sequence of 0 to N emitted items, optionally terminated by either a completion signal or an error. Thus, the possible values of a flux are a value, a completion signal, or an error. As in the Reactive Streams spec, these 3 types of signal translate to calls to a downstream object’s ‘onNext’, ‘onComplete’ or ‘onError’ methods. click here for more detail.
now we have handler and repository we need to expose endpoint, there is the magic of webFlux, you not need to create a separate class for it also get a ride from so much boilerplate, see below snippet.
Let me explain line by line,
@Configuration annotation indicates that a class declares one or more
@Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
@EnableReactiveMongoRepositories annotation is for Spring boot to enable reactive mongo repositories. if no base package is configured, in the case of spring boot application it is by default enable if you have include starter library.
@Bean is a method-level annotation and a direct analog of the XML element. The annotation supports most of the attributes offered by.
RouterFunction Central entry point to Spring’s functional web framework. Exposes routing functionality, such as to create a RouterFunction given a RequestPredicate and HandlerFunction, and to do further subroutine on an existing routing function.
HandlerFunction Incoming HTTP requests are handled by a HandlerFunction, which is essentially a function that takes a ServerRequest and returns a Mono
let’s test whatever we have written, see below code snippet.
By default Spring webFulx include asynchronous natty server.
@RunWith(SpringRunner::class) Indicates that the class should use Spring’s JUnit facilities.
@SpringBootTest provides the following features over and above the regular Spring TestContext Framework,
WebTestClient Non-blocking, reactive client for testing web servers. It uses the reactive
WebClient internally to perform requests and provides a fluent API to verify responses.
WebTestClient can connect to any server over an HTTP connection. It can also bind directly to WebFlux applications using mock request and response objects, without the need for an HTTP server.
There are lots more features provided by Spring 5 and project reactor, I’ve introduced very basic sample on it. sample source available on my github .