Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SpringBootとKtorを雑に比較してみる

 SpringBootとKtorを雑に比較してみる

Ae80e3cf6ad7af954086e8c30192a289?s=128

てっしー

March 25, 2020
Tweet

Transcript

  1. SpringBootͱKtorΛ ࡶʹൺֱͯ͠ΈΔ ͳ͕ͷJava #1 2020.03.25 @chichi1091

  2. SpringBootͱKtorΛ ࡶʹൺֱͯ͠ΈΔ ͳ͕ͷJava #1 2020.03.25 @chichi1091

  3. ࣗݾ঺հ • ࣉౢઍ੖ʗͯͬ͠ʔʢ@chichi1091ʣ • גࣜձࣾHolmes • αʔόαΠυΤϯδχΞ • SpringBoot(Java,Kotlin),Groovy https://chichi1091.hatenablog.jp/

    https://github.com/chichi1091
  4. © Holmes,inc. All right reserved. ܖ໿ϚωδϝϯτγεςϜ
 ʮϗʔϜζΫϥ΢υʯ ܖ໿ʹɺ Ϛωδϝϯτͷ
 ࢹ఺Λɻ

  5. © Holmes,inc. All right reserved. ։ൃ؀ڥ

  6. ։ൃʹؔΘΔશ৬छੵۃ࠾༻தͰ͢ʂ – ϓϩμΫτϚωʔδϟ – UI/UXσβΠφʔ – webϑϩϯτΤϯυΤϯδχΞ – αʔόαΠυΤϯδχΞ –

    SRE
  7. Ktorͱ͸ʁ

  8. Ktorͱ͸ʁ • KotlinΛ։ൃ͍ͯ͠ΔJetBrains͕ࣾ։ൃͨܰ͠ྔ WebϑϨʔϜϫʔΫ • featureΛ௥Ճ͢Δ͜ͱͰ֦ுػೳͷར༻͕Մೳ • ςϯϓϨʔτΤϯδϯ • ೝূػೳ

    • DI
  9. SpringBootͱൺֱ

  10. SpringBootͱൺֱ • ීஈ࢖͍ͬͯΔSpringBootͱൺֱ͢Δ͜ͱͰཧղ ΛਂΊ͔ͨͬͨ • 100%Kotlin੡ͷϑϨʔϜϫʔΫʹڵຯ͕͋ͬͨ • ӡ༻ʹ଱͑ΒΕΔ͔ௐ΂͔ͨͬͨ

  11. ૊ΈࠐΈAPαʔό

  12. ૊ΈࠐΈAPαʔόʢSpringBootʣ • Buildπʔϧʹspring-boot-starter-xxxΛ૊ΈࠐΉ ͜ͱͰ੾Γସ͕͑Մೳ • tomcat • jetty • undertow

  13. ૊ΈࠐΈAPαʔόʢKtorʣ • Buildπʔϧʹktor-server-xxxΛ૊ΈࠐΉ͜ͱͰ੾ Γସ͕͑Մೳ • Netty • jetty • tomcat

  14. BootRun

  15. BootRun @SpringBootApplication @Controller class Application { @GetMapping("/") fun index(): String

    = "Hello, Kotlin" } fun main(args: Array<String>) { runApplication<BlogApplication>(*args) } class Application fun main(args: Array<String>) { val server = embeddedServer(Netty, 8080) { routing { get("/") { call.respond(HttpStatusCode.OK,"Hello, Kotlin") } } } server.start() } 4QSJOH#PPU ,UPS
  16. ςϯϓϨʔτΤϯδϯ

  17. ςϯϓϨʔτΤϯδϯ • SpringBoot • Thymeleaf • Freemarker • Groovy Template

    • Ktor • Thymeleaf • Freemarker • Velocity • Mustache • Kotlin DSLͰHTML΍CSSΛॻ͘͜ͱ΋Մೳ
  18. ςϯϓϨʔτΤϯδϯʢKotlin DSLʣ get("/styles.css") { call.respondCss { body { backgroundColor =

    Color.red } p { fontSize = 2.em } rule("p.myclass") { color = Color.blue } } } suspend inline fun ApplicationCall.respondCss(builder: CSSBuilder.() -> Unit) { this.respondText(CSSBuilder().apply(builder).toString(), ContentType.Text.CSS) } w ,PUMJOίʔυͰ$44Λੜ੒ɾग़ྗ
  19. ςϯϓϨʔτΤϯδϯʢKotlin DSLʣ get("/") { call.respondHtml { head { link(rel =

    "stylesheet", href = "/styles.css", type = "text/css") } body{ h1 { id = "title" +"ͳ͕ͷJava" } p("myclass") { +"myclass" } ul { for(n in 1..10) { li { onClick = "alert($n)" +"$n" } } } } } } w ,PUMJOίʔυͰ)5.-Λੜ੒ɾग़ྗ
  20. Controller

  21. ControllerʢSpringBootʣ @Controller //@RestController @RequestMapping("/users") class UsersApplication{ @GetMapping("/") fun list(): String

    = "users routing ok" @GetMapping("/{id}") fun index(@PathVariable(“id") id: String): String = "user $id routing ok" }
  22. ControllerʢKtorʣ fun main(args: Array<String>) { val server = embeddedServer(Netty, 8080)

    { routing { userController() } } server.start() } fun Route.userController() { route("/users") { get() { call.respondText { "users routing ok" } } get("/{id}") { val id = call.parameters["id"] call.respond(HttpStatusCode.OK, "user $id routing ok") } } } w SPVUFͰΤϯυϙΠϯτΛࢦఆ w ͦͷதʹϧʔςΟϯάઃఆ͠ΞΫγϣ ϯΛࢦఆ͢Δ
  23. ྫ֎ϋϯυϥʔ

  24. ControllerʢSpringBootʣ class ExceptionHandler: ResponseEntityExceptionHandler() { @ExceptionHandler(Exception::class) fun handleException(ex: Exception, headers:

    HttpHeaders, request: WebRequest): ResponseEntity<Any> { return super.handleExceptionInternal(ex, "handleException", headers , HttpStatus.INTERNAL_SERVER_ERROR, request) } }
  25. ControllerʢKtorʣ fun main() { val server = embeddedServer(Netty, port =

    8082) { install(StatusPages) { exception<XxxException>{ cause -> call.respond(HttpStatusCode.InternalServerError) } exception<AuthorizationException>{ cause -> call.respond(HttpStatusCode.Forbidden) } } routing { userController() } } server.start() } class XxxException: RuntimeException() class AuthorizationException: RuntimeException() w ྫ֎ʹΑͬͯฦ͢εςʔλείʔυΛ ఆٛ
  26. Session

  27. SessionʢSpringBootʣ @Controller //@RestController @RequestMapping("/users") class UsersApplication{ @GetMapping("/") fun list(request: HttpServletRequest):

    String { val session = request.session session.setAttribute("key", "name") val name = session.getAttribute("key") return "users routing ok" } } w Լهͷྫ͸௚઀4FTTJPOΛར༻͢Δํ๏ w ଞʹ΋ w !4FTTJPO"UUSJCVUFT w !4DPQF ηογϣϯείʔϓ
  28. SessionʢKtorʣ fun main() { val server = embeddedServer(Netty, port =

    8082) { install(Sessions) { cookie<MySession>("MY_SESSION") { cookie.extensions["SameSite"] = "lax" } } routing { get() { val session: MySession = call.sessions.get<MySession>() ?: MySession("1") call.sessions.set("MY_SESSION", session.copy("2")) call.respondText { "users routing ${session.id}" } } } } server.start() } data class MySession ( val id: String ) w ʮ.:@4&44*0/ʯ͕$PPLJF໊
  29. DB઀ଓ

  30. DB઀ଓʢSpringBootʣ @Entity data class Users ( @Id @GeneratedValue(strategy=GenerationType.IDENTITY) val id:

    Long, val name: String, val birthday: Date ) @Repository interface UsersRepository: JpaRepository<Users, Long> {} @RestController class UsersController( private val usersRepository: UsersRepository ) { @GetMapping(value = ["/"]) @ResponseStatus(HttpStatus.OK) fun index(): List<Users> = usersRepository.findAll() } • ORϚούʔ͸JPA • τϥϯβΫγϣϯ͸AOPͰ੍ޚ
  31. DB઀ଓʢKtorʣ • ORϚούʔ͸Exposed • ΋ͪΖΜKotlin੡ • JetBrains͕ࣾ։ൃ • BootRun࣌ʹH2΁઀ଓ fun

    main(args: Array<String>) { // DB΁઀ଓ Database.connect("jdbc:h2:mem:ktor_db;DB_CLOSE_DELAY=-1", "org.h2.Driver") val server = embeddedServer(Netty, 8080) { routing { userController() } } server.start() }
  32. DB઀ଓʢKtorʣ // Dao object Users : LongIdTable() { val name:

    Column<String> = varchar("name", 50) val birthday: Column<DateTime> = date("birthday") } // Entity class User(id: EntityID<Long>): LongEntity(id) { companion object : LongEntityClass<User>(Users) var name by Users.name var birthday by Users.birthday } fun Route.userController() { val service by inject<UserService>() route("/users") { post() { SchemaUtils.create(Users) // ϚΠάϨʔγϣϯ val cal = Calendar.getInstance() cal.set(Calendar.YEAR, 2020) cal.set(Calendar.MONTH, 3 -1) cal.set(Calendar.DAY_OF_MONTH, 25) val today = DateTime(Calendar.getInstance().timeInMillis) transaction { User.new { name = "kotlin ktor" birthday = DateTime(today) } } call.respondText { "users insert" } } put() { transaction { Users.update ({ Users.id eq 1 }) { it[Users.name] = "kotlin OExposed" } } call.respondText { "users update" } } delete() { transaction { Users.deleteWhere { Users.id eq 1 } } call.respondText { "users delete" } } get() { var result: List<User> = listOf() transaction { result = User.all().sortedByDescending{ it.birthday } } call.respondText { "users routing ${service.getName()}" } } } } w 42-ൃߦ࣌͸USBOTBDUJPOඞਢ w ݕࡧ͸ଞʹ΋ val user: SizedIterable<User> = User.find { Users.id eq 1 } val user: User? = User.findById(1L)
  33. JSON

  34. JSONʢSpringBootʣ @RestController @RequestMapping("/users") class UsersApplication{ @GetMapping("/") fun list(request: HttpServletRequest): ResponseEntity<Json>

    { val json = Json(1,"Kotlin") return ResponseEntity.of(Optional.of(json)) } } data class Json( private val id: Int, private val name: String )
  35. JSONʢKtorʣ fun main() { val server = embeddedServer(Netty, port =

    8082) { install(ContentNegotiation) { jackson { configure(SerializationFeature.INDENT_OUTPUT, true) } } routing { get() { call.respond(Json(1, "Ktor")) } } } server.start() } data class Json( private val id: Int, private val name: String ) w JOTUBMMͰ+BDLTPOͷઃఆ w ฦ٫͞ΕΔ+40/Λ੔ܗ
  36. DI

  37. DIʢSpringBootʣ @Service class UserService { fun getName(): String { return

    "Kotlin" } } @RestController class UserController( private val userService: UserService ) { @GetMapping("/") fun index(): String { return "Hello, ${userService.getName()}" } }
  38. DIʢKtorʣ • KtorͰDI͢Δʹ͸ʮKodeinʯ͔ʮKoinʯΛ࢖͏ • ࠓճ͸Koin fun main() { val server

    = embeddedServer(Netty, port = 8082) { install(Koin) { modules(module) } routing { userController() } } server.start() } val module = module(createdAtStart = true) { singleBy<UserService, UserService>() } class UserService { fun getName(): String{ return "Kotlin" } } fun Route.userController() { val service by inject<UserService>() route("/users") { get() { call.respondText { "users routing ${service.getName()}" } } } } w JOTUBMM ,PJO Ͱ%*ίϯςφ΁ొ࿥ w γϯάϧτϯͰ6TFS4FSWJDFΛొ࿥ w JOKFDUͰऔಘ
  39. ·ͱΊ

  40. ·ͱΊ • SpringBootͱಉ౳ͷfeature͕ἧ͍ͬͯΔΑ͏ʹ ײͨ͡ • υΩϡϝϯτ΋Θ͔Γ΍͍͢(https://ktor.io/) • Githubͷߋ৽΋׆ൃ • feature΋ͲΜͲΜ૿͑ͦ͏ʁ

    • SpringBootΑΓKotlinͬΆ͍ίʔυ͕ॻ͚Δ • ຊ൪ར༻΋଱͑ΒΕͦ͏