Spring 5 reactive web application using kotlin

Spring has introduced webFlux new reactive web framework to support reactive programming.

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) .

Prerequisites :

Spring boot
Spring 5 webFlux and Reactive basic(RxJava or Project Reactor)
Spring data

let generate spring boot template from Spring Initializr

Screen Shot 2017-11-26 at 11.13.34 AM

now import the generated project in your IDE and create Customer data class, check below example:-

Screen Shot 2017-11-26 at 11.35.48 AM

git link

@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.

Screen Shot 2017-11-26 at 10.09.42 AM

git link

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.

Screen Shot 2017-11-26 at 10.08.20 AM

git link

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.

Screen Shot 2017-11-26 at 10.07.39 AM

git link

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.

Screen Shot 2017-11-28 at 4.59.39 AM

git link

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,

  • Uses SpringBootContextLoader as the default ContextLoader when no specific @ContextConfiguration(loader=…) is defined.
  • Automatically searches for a @SpringBootConfiguration when nested @Configuration is not used, and no explicit classes are specified.
  • Allows custom Environment properties to be defined using the properties attribute.
  • Provides support for different webEnvironment modes, including the ability to start a fully running container listening on a defined or random port.
  • Registers a TestRestTemplate bean for use in web tests that are using a fully running container.

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 .


One thought on “Spring 5 reactive web application using kotlin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s