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 {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
		classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
		classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
                classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}")
	}
}

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 {
	mavenCentral()
}


dependencies {
	compile('org.springframework.boot:spring-boot-starter-data-jpa')
	compile('org.springframework.boot:spring-boot-starter-mustache')
	compile('org.springframework.boot:spring-boot-starter-web')
	compile('com.fasterxml.jackson.module:jackson-module-kotlin')
	compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	compile("org.jetbrains.kotlin:kotlin-reflect")
	runtime('com.h2database:h2')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

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:

@SpringBootApplication
class SpringBootKotlinDemoApplication

fun main(args: Array<String>) {
    runApplication<SpringBootKotlinDemoApplication>(*args)
}

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

@Entity
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):

@Repository
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 = repository.save(t)
    fun delete(id: Long) = repository.deleteById(id)
    fun count(): Long = repository.count()
}
@Service
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 = service.save(t)
    @PutMapping            fun update(@RequestBody t: T): T = service.save(t)
    @DeleteMapping("{id}") fun delete(@PathVariable id: Long) = service.delete(id)
    @GetMapping("count")   fun count(): Long = service.count()
}
@RestController
@RequestMapping("users")
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