Introduction
Scala, Groovy and Clojure
Write a Kest application in Scala, Groovy or Clojure: setup, a working example for each, and the differences.
These three languages are wired when the application starts, not at build time. There is no processor to add: depend on the reflect package (or clojure, which includes it) and write the same annotations you would write in Java.
Shared setup
// build.gradle.kts
dependencies {
implementation("io.kest.framework:core")
implementation("io.kest.framework:http-server")
implementation("io.kest.framework:http-server-jdk") // or -netty
implementation("io.kest.framework:json-jackson")
implementation("io.kest.framework:validation")
implementation("io.kest.framework:reflect") // reads the annotations at start-up
}Start it with Kest.run(AppModule.class): Kest finds the module's descriptor through the reflect package (no generated class exists for it), then you start the HTTP server as in any application.
Two rules are different from Kotlin, because Scala and Groovy do not keep parameter names:
- Name every route parameter:
@Param("id"),@Query("limit"),@Header("x-key"). Kest tells you which method is missing a name. - A parameter is optional when it is a
java.util.Optional, or carries any annotation calledNullable.
Scala
case class NewBook(@(NotBlank @field) title: String, @(Min @field)(1) pages: Int)
@Injectable class Library { /* ... */ }
@Controller("/books")
class BookController(library: Library) {
@Get("/{id}") def find(@Param("id") id: Int, @Query("verbose") verbose: java.util.Optional[java.lang.Boolean]): String = s"book $id"
@Post("") @HttpCode(201) def add(@Validated(groups = Array[Class[_]]()) @Body book: NewBook): java.util.Map[String, Int] = ...
}
@Module(imports = Array(classOf[JacksonJsonModule], classOf[ValidationModule]), providers = Array(classOf[Library]), controllers = Array(classOf[BookController]))
class AppModuleNotes: use the scala Gradle plugin and add com.fasterxml.jackson.module:jackson-module-scala_2.13 so JSON can read case classes (Kest registers Jackson modules found on the classpath). Put validation annotations on the field with @(NotBlank @field). A Scala annotation needs its arguments spelled out: @Validated(groups = Array[Class[_]]()). Complete example: examples/scala-api.
Groovy
class NewNote { @NotBlank String text; @Min(1L) int priority }
@Controller("/notes")
class NoteController {
private final Notes notes
NoteController(Notes notes) { this.notes = notes }
@Get("/{id}") String find(@Param("id") int id, @Query("tag") String tag) { "note $id" }
@Post("") @HttpCode(201) Map<String, Integer> add(@Validated @Body NewNote note) { [total: notes.add(note)] }
}
@Module(imports = [JacksonJsonModule, ValidationModule], providers = [Notes], controllers = [NoteController])
class AppModule {}Use the groovy Gradle plugin and org.apache.groovy:groovy. Groovy properties work as DTO fields. Complete example: examples/groovy-api.
Clojure
Clojure has no convenient annotation syntax, so a module is described as data and functions with the kest.core namespace (add the clojure package):
(ns demo.app (:require [kest.core :as kest]) (:import [io.kest.json.jackson JacksonJsonModule]))
(definterface App)
(deftype Counter [^java.util.concurrent.atomic.AtomicInteger n])
(deftype CounterCtl [^Counter counter])
(def module
(kest/module App
{:imports [JacksonJsonModule]
:providers [{:type Counter :factory (fn [_] (Counter. (java.util.concurrent.atomic.AtomicInteger.)))}]
:controllers [{:type CounterCtl :path "/count" :factory (fn [r] (CounterCtl. (kest/get r Counter)))
:routes [{:method :get :path "/{name}" :handler (fn [self ctx] {:name (kest/path ctx "name") :tag (kest/query ctx "tag")})}
{:method :post :path "" :status 201 :handler (fn [self ctx] {:got (get (kest/body-as ctx java.util.Map) "x")})}]}]}))
(kest/run module) ; then start the server with KestHttp.startThe module's name is any class (definterface is enough). Handlers receive the controller instance and the request context, and return data; keyword keys become JSON strings. Helpers: kest/get, kest/path, kest/query, kest/header, kest/body-as. Complete test: packages/clojure.
The same builder is available to Java and Kotlin as ModuleBuilder, if you want to assemble a module in code.
What is tested
Each language has a test that starts the real application, serves it on a port, and checks status codes, bodies and validation over HTTP (Scala 1, Groovy 1, Clojure 1, and a Kotlin test of the reader itself). Guards, interceptors and the rest of the decorators are read by the same code that Kotlin and Java use through the processor's rules, but they are covered by the Kotlin and Java suites rather than repeated per language.