Javalin is a very popular, simple, lightweight and flexible micro web framework for Java and Kotlin. It's un-opinionated and runs on top of Jetty, one of the most used and stable web-servers for the JVM.
Build configuration
The integration with Javalin is contained in the kvision-server-javalin module. It has to be added as the dependency in the common target. This module depends on thejackson-module-kotlin and guice libraries. Any other dependencies can be added to build.gradle.kts and then be used in your application.
The implementation of the service class comes down to implementing required interface methods.
@Suppress("ACTUAL_WITHOUT_EXPECT")
actual class AddressService : IAddressService {
override suspend fun getAddressList(search: String?, sort: Sort) {
return listOf()
}
override suspend fun addAddress(address: Address) {
return Address()
}
override suspend fun updateAddress(id: Int, address: Address) {
return Address()
}
override suspend fun deleteAddress(id: Int) {
return false
}
}
The integration module utilizes Guice and you can access external components and resources by injecting object instances into your class. KVision allows you to inject Javalin instance, which give you access to the application configuration and also Context object, which allows you to access the current request and session information.
@Suppress("ACTUAL_WITHOUT_EXPECT")
actual class AddressService : IAddressService {
@Inject
lateinit var javalin: Javalin
@Inject
lateinit var ctx: Context
override suspend fun getAddressList(search: String?, sort: Sort) {
println(ctx.req.remoteAddr)
println(ctx.req.session.id)
return listOf()
}
// ...
}
You can also inject other Guice components, defined in your application and configured in custom Guice modules or with just-in-time bindings.
Note: The new instance of the service class will be created by Guice for every server request. Use session or request objects to store your state with appropriate scope.
The main function
This function is the application starting point. It's used to initialize and configure the application. Minimal implementation for KVision integration contains kvisionInit and applyRoutes function calls.