Android does not happen much, but let’s take a look on Kotlin in other platforms. For example, take Rest-service, a classic Enterprise-solution.

For beginning, that’s my make-file (I deside to use gradle):

buildscript {
	ext {
		kotlinVersion = '1.2.71'
		springBootVersion = '2.0.5.RELEASE'
	repositories {
	dependencies {

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: "kotlin-jpa"

group = 'com.criticalgnome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
	kotlinOptions {
		freeCompilerArgs = ["-Xjsr305=strict"]
		jvmTarget = "1.8"
compileTestKotlin {
	kotlinOptions {
		freeCompilerArgs = ["-Xjsr305=strict"]
		jvmTarget = "1.8"

repositories {

dependencies {

A few comments: using SpringBoot with Web, Mustache and H2 database plugins. Added plugin kotlin-jpa for creating empty constructors in entity-objects. This need for Hibernate successful working.

Now starts. Running class is very simple:

class SpringBootKotlinDemoApplication

fun main(args: Array<String>) {

Now writing entity-object. It wil be very simple for our example, but can support all dependencies, as complicated as you like:

data class User(
        @Id @GeneratedValue val id: Long,
        val firstName: String,
        val lastName: String

Adding repository. Like using Java, it very minimalistic (for default CRUD-operations):

interface UserRepository: JpaRepository<User, Long>

Now we going to business-logic. For some reason, I decided to do it according to my mind and immediately put the standard operations into a separate base class. These guys are:

open class BaseService<T>(private val repository: JpaRepository<T, Long>) {
    fun getOne(id: Long): T = repository.getOne(id)
    fun getAll(pageable: Pageable): Page<T> = repository.findAll(pageable)
    fun save(t: T): T =
    fun delete(id: Long) = repository.deleteById(id)
    fun count(): Long = repository.count()
class UserService(@Autowired private val userRepository: UserRepository): BaseService<User>(userRepository)

Now adding controller. Since I’ve gotten breaking bad for the base class of the service, I’ll do the same trick here:

open class BaseController<T>(private val service: BaseService<T>) {
    @GetMapping("{id}")    fun getOne(@PathVariable id: Long): T = service.getOne(id)
    @GetMapping            fun getAll(pageable: Pageable): Page<T> = service.getAll(pageable)
    @PostMapping           fun create(@RequestBody t: T): T =
    @PutMapping            fun update(@RequestBody t: T): T =
    @DeleteMapping("{id}") fun delete(@PathVariable id: Long) = service.delete(id)
    @GetMapping("count")   fun count(): Long = service.count()
class UserController(@Autowired private val userService: UserService): BaseController<User, Long>(userService)

And tha’t all, folks. Run and enjoy. All code on my GitLab, since GitHub working strange today 🙁

Leave a Reply

Close Menu