springspring-webfluxspring-dsl

Non-reactive Spring web with kotlin functional router


Can I use new Kotlin DSL for setting up routing, for example as in :

router {
    ("/blog" and accept(TEXT_HTML)).nest {
        GET("/", fooHandler::findAllView)
        GET("/{slug}", fooHandler::findOneView)
    }
    ("/api/blog" and accept(APPLICATION_JSON)).nest {
        GET("/", barHandler::findAll)
        GET("/{id}", barHandler::findOne)
    }
}

with non-reactive web part? In the sense that underlying database will be Postgres and non Reactive servlet based application server so I do not want/need to use Flux or Mono for return types of barHandler or repository functions. But I do like new router DSL when used with Kotlin and it is more powerful than annotation based @RequestMapping and easier to get a grasp of all the app routes.


Solution

  • The DSL in your example is part of Spring WebFlux, which is the "reactive" thing you speak of. In this official blog post, the functionality is being introduced as "Spring WebFlux functional DSL".

    The DSL entry point router is defined in the package org.springframework.web.reactive.function.server which also verifies what I said earlier. You can have a look at it on GitHub.

    What you can use with traditional Web MVC though: The Functional bean declaration DSL that is used to define your application beans in a way that looks as follows:

    beans {
        bean<Foo>()
        bean { Bar(ref()) }
    }