9. KotlinConf 2018 から⾒る最近の Kotlin サーバーサイド事情
http4k
val app = routes(
"bob" bind GET to { Response(OK).body("you GET bob") },
"rita" bind POST to { Response(OK).body("you POST rita") },
"sue" bind DELETE to { Response(OK).body("you DELETE sue") }
)
val server = app.asServer(SunHttp(8000)).start()
Ktor
embeddedServer(Netty, 8080) {
routing {
get("/") {
call.respondText("Hello, world!", ContentType.Text.Html)
}
}
}.start(wait = true)
Server as a Function
10. KotlinConf 2018 から⾒る最近の Kotlin サーバーサイド事情
Spring Boot を Kotlin で快適に使うには…
•SpringFu, KoFu を使って Functional!! DSL!! (ただし incubating feature)
• やっぱり Spring 使いたいですよね
• https://github.com/spring-projects/spring-fu
•Annotation を多⽤した Configuration, Bean registration は⿊魔術に近い
• 何が起きているのかわからない、辿れない
• 解決策としての Functional configuration with Kotlin DSL で明⽰的な Configuration を実現
• 上記のプレゼンでは、Live coding で1つづつアノテーションを削っていく実演
from Kotlin and Spring Boot, a Match Made in Heaven
11. KotlinConf 2018 から⾒る最近の Kotlin サーバーサイド事情
SpringFu, KoFu のサンプル https://github.com/ajavageek/springfunctional-migration
@SpringBootApplication
class MigrationDemoApplication
fun main(args: Array<String>) {
runApplication<MigrationDemoApplication>(*args)
}
@RestController
class PersonController(private val personRepository: PersonRepository) {
@GetMapping("/person")
fun readAll() = personRepository.findAll()
@GetMapping("/person/{id}")
fun readOne(@PathVariable id: Long) = personRepository.findById(id)
}
interface PersonRepository : PagingAndSortingRepository<Person, Long>