LoginSignup
16

More than 5 years have passed since last update.

Kotlin × SpringBootでHelloWorldしてみる

Last updated at Posted at 2019-01-24

背景

  • SpringBootとJavaでしばらく遊んでたので今度はSpringBootとKotlinでも動かしてみたいと思った
  • Kotlinのコードは全然わからない

とりあえずやってみた

用意したもの

  • IntelliJ Idea
  • Java1.8
  • kotlin1.2.71
  • SpringBoot2.0.4

IntelliJからプロジェクトを作成する

  • Spring Initializrからプロジェクトを作成
    image.png

  • languageをkotlinにする
    image.png

  • よしなにライブラリを追加する
    image.png

  • あとはUse Auto Importするだけ

image.png

Hello Worldしてみる

SampleController.kt
package com.example.sample_kotlin

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping
class SampleController {

    @GetMapping("/hello")
    fun getHello() :String {
        return "Hello World"
    }
}
  • 叩いてみる
% curl localhost:8080/hello
   Hello World%                

今度はパラメータを受け取ってみる

SampleController.kt
package com.example.sample_kotlin

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping
class SampleController {

    @GetMapping("/hello")
    fun getHello(@RequestParam name: String) :String {
        return "Hello $name"
    }
}
  • 叩いてみる
% curl "http://localhost:8080/hello?name=Tom"
   Hello Tom%                

Responseを作成してみる

SampleController.kt
package com.example.sample_kotlin

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping
class SampleController {

    @GetMapping("/name")
    fun getName() :SampleResponse {
        return SampleResponse().apply {
            familyName = "トム"
            lasName = "ブラウン"
            age = 20
            sex = "male"
        }
    }
}
SampleResponse.kt
package com.example.sample_kotlin

class SampleResponse {
    var familyName = String()
    var lasName = String()
    var age : Int = 0
    var sex = String()
}
  • 叩いてみる
% curl "http://localhost:8080/name"
   {"familyName":"トム","lasName":"ブラウン","age":20,"sex":"male"}%                

それなりに楽しくなってきたので、DBを用意する

  • mysqlは適当に用意してください

まず、apllication.ymlでつなぎ先を設定

apllication.yml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/sample?useSSL=false
    username: sample
    password: secret

Jpaでつなぎたいのでgradleに設定を追加

build.gradle
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtime 'mysql:mysql-connector-java'

Entityをつくる(Entityはテーブルの中身と一致させる必要があります)

SampleEntity.kt
package com.example.sample_kotlin

import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table

@Entity
@Table(name = "sample")
class SampleEntity {
    @Id
    var id : Int = 0
    @Column(name = "family_name")
    var familyName = String()
    @Column(name = "last_name")
    var lastName = String()
    var age : Int = 0
    var sex = String()
}
  • Jpaを継承したRepositoryを実装する
SampleRepository.kt
package com.example.sample_kotlin

import org.springframework.data.jpa.repository.JpaRepository

interface SampleRepository: JpaRepository<SampleEntity,String>
  • コントローラの修正(めんどくさいんで直でEntity返すようにしちゃってます)
SampleController.kt
package com.example.sample_kotlin

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping
class SampleController(val sampleRepository: SampleRepository) {

    @GetMapping("/name")
    fun getName() :SampleEntity {
        var responseList : List<SampleEntity> = sampleRepository.findAll()
        return responseList[0]
    }
}
  • 叩いてみる
% curl "http://localhost:8080/name"
   {"id":1,"familyName":"トム","lasName":"ブラウン","age":20,"sex":"male"}%                

感想

  • 1日くらいしかまだ遊んでないですが、結構楽しい
  • Javaやってるとある程度はイメージしやすいところがある
  • O/RマッパーはJPAを今までも使っていて、Kotlinでも普通に使えた
  • 次回は簡単なアプリケーションを動かす予定です

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16