본문으로 바로가기
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.



Spring boot를 개발하는데 Kotlin으로 개발을 하더라.. 덕분에 Kotlin을 공부하게 되었고, 코틀린의 장점들을 많이 배웠다. 

코드도 많이 간결해지며, NullType Check, 끝에 ; 붙이지 않아도 되고.. 좋은 것 같다.


그래서 이번 포스팅에서는 Spring boot에서 Kotlin을 적용하여 Controller을 만들어 적용하는 방법에 대해서 간략한 포스팅을 작성해보았다. 적용하는 방법 외에 Kotlin Test 및 다양한 내용에 대해서 정리된 블로그도 있는데 참고하는 것도 좋을 것 같다.


나는 Gradle을 사용하며 Multi-Module을 사용하였기 때문에 이 블로그를 보고 적용하는 사람은 이게 뭐지? 할 수도 있는데 63번 라인 뒤에 있는 것들은 무시하도록 하자.


build.gradle에서 추가할 내용이 있다.

아래 11 ~ 13 라인 처럼 kotlin dependencies를 추가하여야 한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
buildscript {
    ext {
        springBootVersion = '2.0.6.RELEASE'
        kotlinVersion = "1.3.11"
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
        classpath "org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}"
        classpath "io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE"
    }
}



그리고 6 ~ 8 라인처럼 Kotlin plugin을 추가하자. 그리고 43 ~ 49라인


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
subprojects {
 
    apply plugin: 'java'
    apply plugin: 'eclipse'
 
    apply plugin: "kotlin"
    apply plugin: "kotlin-spring"
    apply plugin: "kotlin-noarg"
 
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
 
    group = 'com.ktko.admin'
    version = '0.0.0'
    sourceCompatibility = 1.8
 
    repositories {
        mavenCentral()
    }
 
 
    task initSourceFolders {
        sourceSets*.java.srcDirs*.each {
            if!it.exists() ) {
                it.mkdirs()
            }
        }
 
        sourceSets*.resources.srcDirs*.each {
            if!it.exists() ) {
                it.mkdirs()
            }
        }
    }
 
    dependencies {
        compileOnly('org.projectlombok:lombok')
        implementation 'org.springframework.boot:spring-boot-starter-aop'
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-security'
        implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
 
        compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
        compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
        compile("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
        testCompile "org.jetbrains.kotlin:kotlin-test:$kotlinVersion"
        testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
        compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.2")
        compile("io.github.microutils:kotlin-logging:1.4.9")
 
        implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'
        implementation 'org.springframework.boot:spring-boot-devtools'
        runtimeOnly 'mysql:mysql-connector-java'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testImplementation 'org.springframework.security:spring-security-test'
        implementation "org.springframework.boot:spring-boot-configuration-processor"
        implementation group: 'org.javassist', name: 'javassist', version: '3.15.0-GA'
    }
}
 
project(':admin-api') {
    dependencies {
        compile project(':admin-common')
    }
}
 
project(':admin-web') {
    dependencies {
        compile project(':admin-common')
    }
}




위의 화면처럼 gradle 셋팅이 끝났다면 이제 코틀린을 사용할 수 있다. Controller을 만들어보자.


@Controller
@RequestMapping("/sign")
class SignController1 {

@GetMapping("/in")
fun logIn(): String {
println("Kotlin Success")

return "sign/login"
}
}


컨트롤러를 만들었다. 이제 실행을 하면 페이지가 뜨는 것을 확인할 수 있었다.