Skip to content

Commit 82a4d03

Browse files
committed
Kotlin + Spring demo
1 parent 0761a17 commit 82a4d03

13 files changed

Lines changed: 555 additions & 0 deletions

File tree

20211018/spring/.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
bin/
16+
!**/src/main/**/bin/
17+
!**/src/test/**/bin/
18+
19+
### IntelliJ IDEA ###
20+
.idea
21+
*.iws
22+
*.iml
23+
*.ipr
24+
out/
25+
!**/src/main/**/out/
26+
!**/src/test/**/out/
27+
28+
### NetBeans ###
29+
/nbproject/private/
30+
/nbbuild/
31+
/dist/
32+
/nbdist/
33+
/.nb-gradle/
34+
35+
### VS Code ###
36+
.vscode/

20211018/spring/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Kotlin + Spring demo
2+
3+
## 실행
4+
5+
Run tests:
6+
7+
```bash
8+
./gradlew test
9+
```
10+
11+
Build JAR:
12+
13+
```bash
14+
./gradlew bootJar
15+
```
16+
17+
Run web server:
18+
19+
```bash
20+
./gradlew bootRun
21+
```
22+
23+
<http://localhost:8080/>
24+
25+
## 기본 코드 생성
26+
27+
Spring Initializr 사용:
28+
<https://start.spring.io/>
29+
30+
## `WelcomeController` 테스트 및 구현 작성
31+
32+
참고:
33+
34+
- [Kotlin + Spring Boot 맛보기](https://github.com/ahastudio/til/blob/main/spring/2019-12-04-kotlin-spring.md)
35+
- [Building web applications with Spring Boot and Kotlin](https://spring.io/guides/tutorials/spring-boot-kotlin/)
36+
37+
`WelcomeControllerTest.kt` 파일 작성:
38+
39+
```kotlin
40+
package com.example.demo.controllers
41+
42+
import org.hamcrest.CoreMatchers.containsString
43+
import org.junit.jupiter.api.Test
44+
import org.springframework.beans.factory.annotation.Autowired
45+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
46+
import org.springframework.test.web.servlet.MockMvc
47+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
48+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
49+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
50+
51+
@WebMvcTest(WelcomeController::class)
52+
internal class WelcomeControllerTest(@Autowired val mockMvc: MockMvc) {
53+
54+
@Test
55+
fun `GET ∕`() {
56+
mockMvc.perform(get("/"))
57+
.andExpect(status().isOk())
58+
.andExpect(content().string(containsString("Hello")))
59+
}
60+
61+
}
62+
```
63+
64+
메서드 이름에 평범한 slash(`/`) 문자을 넣을 수 없어서
65+
[유니코드 slash(``) 문자](https://www.compart.com/en/unicode/U+2215)
66+
사용함.
67+
68+
`WelcomeController.kt` 파일 작성:
69+
70+
```kotlin
71+
package com.example.demo.controllers
72+
73+
import org.springframework.web.bind.annotation.GetMapping
74+
import org.springframework.web.bind.annotation.RestController
75+
76+
@RestController
77+
class WelcomeController {
78+
79+
@GetMapping
80+
fun home(): String {
81+
return "Hello, world!"
82+
}
83+
84+
}
85+
```

20211018/spring/build.gradle.kts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
3+
plugins {
4+
id("org.springframework.boot") version "2.5.5"
5+
id("io.spring.dependency-management") version "1.0.11.RELEASE"
6+
kotlin("jvm") version "1.5.31"
7+
kotlin("plugin.spring") version "1.5.31"
8+
}
9+
10+
group = "com.example"
11+
version = "0.0.1-SNAPSHOT"
12+
java.sourceCompatibility = JavaVersion.VERSION_11
13+
14+
repositories {
15+
mavenCentral()
16+
}
17+
18+
dependencies {
19+
implementation("org.springframework.boot:spring-boot-starter-jooq")
20+
implementation("org.springframework.boot:spring-boot-starter-validation")
21+
implementation("org.springframework.boot:spring-boot-starter-web")
22+
23+
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
24+
implementation("org.jetbrains.kotlin:kotlin-reflect")
25+
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
26+
27+
developmentOnly("org.springframework.boot:spring-boot-devtools")
28+
29+
runtimeOnly("com.h2database:h2")
30+
runtimeOnly("org.postgresql:postgresql")
31+
32+
testImplementation("org.springframework.boot:spring-boot-starter-test")
33+
}
34+
35+
tasks.withType<KotlinCompile> {
36+
kotlinOptions {
37+
freeCompilerArgs = listOf("-Xjsr305=strict")
38+
jvmTarget = "11"
39+
}
40+
}
41+
42+
tasks.withType<Test> {
43+
useJUnitPlatform()
44+
}
58.1 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)