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

Server Side Kotlin と Spring Boot v2

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
BulBulPaul
August 09, 2018

Server Side Kotlin と Spring Boot v2

Yahoo!カレンダーでのKotlin活用の状況や、Spring Boot v2 でのKotlin対応について発表しました。

BulBulPaul

August 09, 2018

Transcript

49 slides
  1. Server Side Kotlinと Spring Boot v2 Mix Leap Study #21


    2018/08/09 ヤフー株式会社 おかだ のぶお @bulbulpaul
  2. !12 Why Kotlin? • Null Safe • sealed class •

    型推論 • Smart Cast • data classが便利 • IDEのサポートが強⼒ • Javaのエコシステムを使える
  3. !13 Why Kotlin? 制約と規律 • Null Safe • sealed class

    • 型推論 • Smart Cast • data classが便利 • IDEのサポートが強⼒ • Javaのエコシステムを使える
  4. • Null Safe • sealed class • 型推論 • Smart

    Cast • data classが便利 • IDEのサポートが強⼒ • Javaのエコシステムを使える !14 Why Kotlin? ⾼い利便性
  5. !15 Why Kotlin? 巨⼈の肩に乗れる • Null Safe • sealed class

    • 型推論 • Smart Cast • data classが便利 • IDEのサポートが強⼒ • Javaのエコシステムを使える
  6. • Null Safe • sealed class • 型推論 • Smart

    Cast • IDEのサポートが強⼒ • data classが便利 • Javaのエコシステムを使える !16 Why Kotlin? 巨⼈の肩に乗れる ⾼い利便性 制約と規律 開発が楽しい ! ! ※ฐνʔϜͷΤϯδχΞஊ
  7. • Athenz(認証 / 認可) • Pulsar (MQ) • Hystrix
 (サーキットブレーカー)

    Use library • SpringBoot (Framework) • Moshi (Json) Javaライブラリ Kotlin対応ライブラリ !18
  8. • Athenz(認証 / 認可) • Pulsar (MQ) • Hystrix
 (サーキットブレーカー)

    Use library • SpringBoot (Framework) • Moshi (Json) Javaライブラリ Kotlin対応ライブラリ !19
  9. • idiomatic Kotlin code • Annotations • RouterFunctions • Bean

    definition DSL !25 Kotlin support • Extension functions • Reified type parameters
  10. public class Example { public <T> T create(Class<T> clazz) {

    try { return clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); } } !27 example
  11. // Kotlin val example = Example() example.create(Foo::class.java) !28 example //

    Java Example example = new Example() example.create(Foo.class)
  12. val example = Example() example.create(Foo::class.java) !29 Kotlin class Example example

    = new Example() example.create(Foo.class) kotlin.reflection.KClass java.lang.Class
  13. import kotlin.reflect.KClass fun <T: Any> Example.create(kclass: KClass<T>) = create(kclass.java) val

    example = Example() example.create(Foo::class) !31 idiomatic Kotlinらしい書き⽅!!
  14. !34 Examples inline fun <reified T: Any> Example.create() = create(T::class.java)

    val example = Example() val foo = example.create<Foo>() 


 val hoge: Hoge = example.create() 左辺の型推論でも実⾏可能 <>で型を指定して実⾏できる
  15. SpringはClass<T>で受け取る処理が多いので
 細々したところの記載がKotlinらしく書ける !36 in Spring Boot // jdbcTemplate を使う時の従来の書き⽅ val

    count = jdbcTemplate.queryForObject( “SELECT count(*) FROM user”, Long::class.java) // Reified type parameters support val count = jdbcTemplate.queryForObject<Long>( “SELECT count(*) FROM user”) val count: Long = jdbcTemplate.queryForObject( “SELECT count(*) FROM user”)
  16. !38 Examples // 配列のJSONをList型で取得するHTTPリクエスト List<User> users = restTemplate.exchange( “https://example.com/users”, HttpMethod.GET,

    null, new ParameterizedTypeReference<List<User>>() {} ) .getBody(); val users: List<User> = restTemplate.exchange( “https://example.com/users”, HttpMethod.GET)
  17. @Controller class ExampleController(val foo: Foo, val bar: Bar?) { }

    !40 DI 必須のBean オプション Beanが無いとException
  18. @Controller class ExampleController(val foo: Foo, val bar: Bar?) { @GetMapping(“/article”)

    fun article(@RequestParam id: String, @RequestParam tag: String?) } !41 RequestParam オプション 必須 @RequestParam(required = false) String name
  19. @Configuration class Router(private val fooHandler: FooHandler, private val barHandler: BarHandler)

    { @Bean fun apiRouter() = router { accept(APPLICATION_JSON_UTF8).nest { GET("/", fooHandler::getDemo) GET("/foo", fooHandler::findAll) } } } !44 RequestParam Router

More Decks by BulBulPaul

Other Decks in Programming