背景
- SpringBootとJavaでしばらく遊んでたので今度はSpringBootとKotlinでも動かしてみたいと思った
- Kotlinのコードは全然わからない
とりあえずやってみた
用意したもの
- IntelliJ Idea
- Java1.8
- kotlin1.2.71
- SpringBoot2.0.4
IntelliJからプロジェクトを作成する
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でも普通に使えた
- 次回は簡単なアプリケーションを動かす予定です