Why did you create Kotlin Rest Api?
- 자바보다 간결하고, 자바와 호환성이 좋기 때문에
- 많은 Java 개발자들이 Kotlin을 같이 사용하여 개발하는 추세(트랜드) 이기 때문
Why did you use Test Driven Design?
- 코드에 대한 신뢰성을 가질 수 있다.
- 리팩토링 시에 안정성을 확보할 수 있다.
- 개발 및 테스팅에 대한 시간과 비용을 절감할 수 있다.
개발환경
- OS : MacOs
- Tool : Intellij (Kotlin 공식 문서에서는 최신 버전 사용시 Intellij 혹은 Android Studio만 지원)
- DataBase : H2
- Build : Gradle
- Language : Kotlin 1.9.x
- Test : Junit5
- Framework : Spring Boot 3.1.2
- Was : Embedded Tomcat
프로젝트 생성
File -> New -> Project -> Spring Initializr Language : Kotlin Type : Gradle - groovy Group : com.example Artifact : rest-api-kotlin Package name : com.example.app Dependency 추가 (H2, Spring Web, Spring JPA)
설정
application.properties
server.port=9090 # h2 database spring.h2.console.enabled=true spring.h2.console.path=/h2-console spring.datasource.hikari.driver-class-name=org.h2.Driver spring.datasource.hikari.jdbc-url=jdbc:h2:mem:test spring.datasource.hikari.username=user spring.datasource.hikari.password= # jpa spring.jpa.hibernate.ddl-auto=create spring.jpa.generate-ddl=true spring.jpa.show-sql=true spring.jpa.database=h2 spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
build.gradle
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { id 'org.springframework.boot' version '3.2.1' id 'io.spring.dependency-management' version '1.1.4' id 'org.jetbrains.kotlin.jvm' version '1.9.21' id 'org.jetbrains.kotlin.plugin.spring' version '1.9.21' id 'org.jetbrains.kotlin.plugin.jpa' version '1.9.21' } group = 'com.example' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'com.fasterxml.jackson.module:jackson-module-kotlin' implementation 'org.jetbrains.kotlin:kotlin-reflect' implementation 'com.google.code.gson:gson:2.9.0' runtimeOnly 'com.h2database:h2' //junit test 사용을 위해 설정 testImplementation('org.springframework.boot:spring-boot-starter-test'){ exclude module : 'junit' } testImplementation('org.junit.jupiter:junit-jupiter-api') testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine') } tasks.withType(KotlinCompile) { kotlinOptions { freeCompilerArgs += '-Xjsr305=strict' jvmTarget = '17' } } test { useJUnitPlatform() }
후기
- Kotlin은 자바와 호환되기 때문에, 코드 작성시 큰 어려움은 없었던 것 같다. 그러나 아직 문법이나 기능들은 더 많이 공부하고 사용해봐야할 것 같음.
- 초기 세팅 시 이클립스로 삽질하는 바람에 시간이 많이 소요 되었지만, 지금과 같은 간단한 예제는 1시간이면 만들었을 것 같다.
- 다음엔 코틀린에 대한 장점을 부각시키는 기능들을 자바와 비교하는 문서를 작성해볼 예정이다.