Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring WebFlux With Kotlin - Reactive Web

DZone's Guide to

Spring WebFlux With Kotlin - Reactive Web

With the latest updates to Spring, it is now even easier to use Kotlin to create great web applications. Read on to see how!

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Spring - Even Fancier

I’m one of the guys who fell in love with Kotlin and wouldn’t write any more Java; if it was that easy. If you’re not familiar with my articles, have a look at the other Kotlin related posts here.

Besides Kotlin, I’ve always been into Spring ever since I started with Java some years ago. I still like the framework although it’s getting bigger and bigger and you often don’t quite know which features to choose, amongst all the alternatives. As the framework itself is growing, the documentation, which is one of best you’ll ever get to see, also is.

The thing I like most about Spring is that you can focus on your business logic from day one and don’t have much technical, infrastructural stuff to set up before kicking off. Spring does that by encapsulating a lot of boilerplate that’s necessary for certain tasks and provides simple annotations we can apply in order to make use of these features. One of the most famous modules certainly is Spring Web MVC, which is widely used when ever it comes to web services on the JVM.

Reactive Programming - The Non-Blocking Way

You might have noticed that Reactive Programming is getting more attention recently. There are many frameworks emerging that want to encourage this style of programming, namely RxJava, Vert.X or Akka for example. If you’ve never come across these, you can read my post on Kotlin with Vert.X as a first step.

Spring Reactive

What does this have to do with Spring though? Well, of course, there’s yet another library for building reactive systems, which in fact is powered by Spring: Project Reactor. Reactor will be used in the upcoming Spring Release 5.0, coming out as stable in September, hopefully, which is going to introduce a reactive web framework called WebFlux.

This fact on its own is a good reason for me to dive into it as it sounds fairly fantastic knowing Web MVC as Spring’s outstanding module already. But, there’s yet another great reason to take this expansion into account: Spring is greatly supporting Kotlin and will even introduce Kotlin dedicated features with the upcoming major release!

This is going to be achieved by making use of extension functions in order to extend existing APIs and also by introducing Kotlin DSLs, also a topic I’m really interested in, as you can read in my post on creating a DSL with Kotlin. One of these new DSLs goes hand in hand with Spring WebFlux: a functional DSL for describing the WebFlux-backed web service. With that, we want to create a Spring WebFlux with Kotlin app.

This, in fact, is what I am going to present to you in a very short example up next…

Spring WebFlux With Kotlin in Action

Let’s have a look at a very basic application using Spring WebFlux in a Kotlin application. The initial setup can easily be downloaded as a SpringBoot application from Spring Initializr, if you choose Kotlin as the programming language and also enable the "Reactive Web" dependency, which has been available since SpringBoot 2.0.0.

Spring Boot Config Spring WebFlux with Kotlin

As soon as we’ve imported this project into our IDE, we can start with creating a reactive web service.

For the sake of brevity, I chose a very simple, not very useful, example: an internally managed repository of simple Strings, that is populated through the web interface and also is searchable from it.

Thanks to Kotlin, and also Spring, there is not much code to write:

Repo and Handler

@Component
class ReactiveHandler(val repo: StringRepo) {
    fun getText(search: String): Mono<String> =
        repo.get(search).toMono().map { "Result: $it!" }
    fun addText(text: String): Mono<String> =
        repo.add(text).toMono().map { "Result: $it!" }
    fun getAllTexts(): Flux<String> =
        repo.getAll().toFlux().map { "Result: $it" }
}

@Component
class StringRepo {
    private val entities = mutableListOf<String>()
    fun add(s: String) = entities.add(s)
    fun get(s: String) = entities.find { it == s } ?: "not found!"
    fun getAll() = listOf(entities)
}

We just create a repository that maintains a list of Strings and another class ReactiveHandler, which is responsible for delegating to the repository and providing "reactive types" defined in Reactor and mandatory for WebFlux: Flux and Mono (read about them here). Regardless of their intention, have a look at how they are created: toMono() and toFlux() are an example of extension functions added in Spring 5.0, a feature dedicated to Kotlin.

Using the Function DSL

The much more interesting part, though, is where the web routing is defined. This part, in particular, is where the already mentioned functional DSL comes into play. Let’s observe how it works.

Functional WebFlux DSL

@Configuration
class RoutingConfiguration {

    @Bean
    fun routerFunction(handler: ReactiveHandler): 
          RouterFunction<ServerResponse> = router {
        ("/reactive").nest {
            val searchPathName = "search"
            val savePathName = "save"
            GET("/{$searchPathName}") { req ->
                val pathVar = req.pathVariable(searchPathName)
                ServerResponse.ok().body(
                        handler.getText(pathVar)
                )
            }
            GET("/") {
                ServerResponse.ok().body(handler.getAllTexts())
            }
            PUT("/{$savePathName}") { req ->
                val pathVar = req.pathVariable(savePathName)
                ServerResponse.ok().body(
                        handler.addText(pathVar)
                )
            }
        }
    }
}

The router function is the entry point of the new DSL, whose definition you can observe on GitHub. The shown solution is just one out of many possible solutions since the DSL provides more ways you can choose from. With my definition, the server starts a web service under "/reactive" and accepts two GET requests and a PUT request, each of which is delegated to the previously shown ReactiveHandler (see method parameter) before the results are put into a ServerResponse. Of course, you’d have to handle errors in a real-world scenario and "ok" wouldn’t be the only response.

Benefit

If you ask me, this approach is very cleanly structured and even provides the opportunity of using any Kotlin code for defining variables, loops, conditions, whatsoever inside the actual DSL code. Given that, you have a very powerful tool which can be utilized in a very natural programmatic way.

If you'd like to check this out, the code is available in my repository.

Wrap-Up and Perspective

I’ve presented a small project that’s making use of Spring 5.0 and its new module WebFlux in combination with Kotlin. I think the fact that Spring officially uses and supports Kotlin is a very important one, I’d like to emphasize it once again.

Kotlin - It’s Not Only Android!

We all know that Kotlin made its way into Android, which was possible because Google announced the official support a few weeks ago. On the server-side though, people, and especially companies, hesitate when it comes to Kotlin. They tend to have doubts as to whether Kotlin’s really mature enough already.

If you ask me, there’s no good reason for hesitation. Many projects use Kotlin already, frameworks support Kotlin and even extend their libraries with dedicated Kotlin features. Spring, as one of the most common Java frameworks, seems to think the same as they quickly adopted Kotlin as an alternative to Java and Groovy for SpringBoot applications. The most recent developments, which are part of Spring 5.0, are the next step, some of which we’ve observed in this little article.

If you’re the same as me, that is, interested in spreading Kotlin as an alternative to Java, talk about it and tell your colleagues about Spring’s support and what’s actually happening!

Special Thanks

As you can read in this article, Spring’s introducing quite a few Kotlin features. There’s one guy, Sébastien Deleuze, who’s highly responsible for this development in the Spring Framework. He has also been part of talkingkotlin already, as one of the first discussion partners of Hadi Hariri. It’s really great to have such influencers in the Kotlin community, many thanks to you! Keep up the great work.

Last Words

If you like to have a look at my examples, the code is available here: Git. Feel free, to give any feedback, I’m always happy to help. Also, If you like, have a look at my Twitter account and follow if you’re interested in more Kotlin stuff. Thanks a lot.

Simon

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
spring ,java ,kotlin ,reactive ,web dev

Published at DZone with permission of Simon Wirtz. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}