kotlin + sleuth = kleuth 🕵️♂️ . A lightweight, flexible framework for generating Spring REST API routes dynamically.
Visit the docs at alien-head.github.io/kleuth or read on to learn more.
Kleuth uses project folders and naming standards to map functions to web requests.
Example:
Classes annotated with Route
or RequestMethod
are then used by Kleuth to map the path set in the folder structure to functions.
This is what the rest/pizzas/GetPizzas.kt
class in the above example looks like in the "handler" style:
@Route
class GetPizzas(private val service: PizzaService) {
fun handler(): ResponseEntity<List<Pizza>> {
return ResponseEntity.ok(service.getAll())
}
}
Kleuth also contains a number of helper functions to make route classes even more concise. The same route can look like this:
@Route
class GetPizzas(private val service: PizzaService) {
fun handler() = ok { service.getAll() }
}
This is a significant improvement over the standard method of creating Spring RestController
classes,
which can decrease in readability quickly:
@RestController
class PizzaController(private val service: PizzaService) {
@GetMapping("/pizzas")
fun getPizzas(): ResponseEntity<List<Pizza>> {
return ResponseEntity.ok(service.getAll())
}
@PostMapping("/pizzas")
fun createPizza(@RequestBody body: Pizza): ResponseEntity<Pizza> {
return ResponseEntity(HttpStatus.CREATED, service.create(body))
}
// ...
@GetMapping("/pizzas/{pizzaId}")
fun getPizzaById(@PathVariable pizzaId: UUID): ResponseEntity<Pizza> {
return ResponseEntity.ok(service.getById(pizzaId))
}
}
A Kleuth route handler class is also a Spring RestController
. This means Kleuth functions are completely compatible with Spring RequestMappings. These functions make use of PathVariable
, RequestBody
, RequestParam
,
and all other possible Spring function annotations and parameters. If for some reason Kleuth does not support a Spring feature, a RequestMapping
function can co-exist in a Kleuth route handler class.
Kleuth REST API routes are created through the directory structure. No more RequestMappings and monolithic Controller classes (Less annotations too!). This also means less work!
The structure of a Kleuth-mapped Spring REST API is clear from the package view. Immediately understand the flow of a Kleuth REST API.
Kleuth helps enforce clear and concise development practices and project organization.
Check out the docs to learn more!