Skip to content

Commit 404ea81

Browse files
Merge pull request eugenp#1940 from tschiman/master
BAEL-755 Spring Boot Kotlin
2 parents b135086 + 7eb0eaf commit 404ea81

8 files changed

Lines changed: 238 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<module>spring-apache-camel</module>
130130
<module>spring-batch</module>
131131
<module>spring-boot</module>
132+
<module>spring-boot-kotlin</module>
132133
<module>spring-cloud-data-flow</module>
133134
<module>spring-cloud</module>
134135
<module>spring-core</module>

‎spring-boot-kotlin/pom.xml‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>2.0.0.BUILD-SNAPSHOT</version>
11+
<relativePath/> <!-- lookup parent from repository -->
12+
</parent>
13+
14+
<artifactId>spring-boot-kotlin</artifactId>
15+
<version>1.0.0-SNAPSHOT</version>
16+
17+
<properties>
18+
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21+
<java.version>1.8</java.version>
22+
<kotlin.version>1.1.2</kotlin.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.jetbrains.kotlin</groupId>
33+
<artifactId>kotlin-stdlib-jre8</artifactId>
34+
<version>${kotlin.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.jetbrains.kotlin</groupId>
38+
<artifactId>kotlin-reflect</artifactId>
39+
<version>${kotlin.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.fasterxml.jackson.module</groupId>
43+
<artifactId>jackson-module-kotlin</artifactId>
44+
<version>2.8.7</version>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-test</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
56+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-maven-plugin</artifactId>
61+
</plugin>
62+
<plugin>
63+
<artifactId>kotlin-maven-plugin</artifactId>
64+
<groupId>org.jetbrains.kotlin</groupId>
65+
<version>${kotlin.version}</version>
66+
<configuration>
67+
<compilerPlugins>
68+
<plugin>spring</plugin>
69+
</compilerPlugins>
70+
<jvmTarget>1.8</jvmTarget>
71+
</configuration>
72+
<executions>
73+
<execution>
74+
<id>compile</id>
75+
<phase>compile</phase>
76+
<goals>
77+
<goal>compile</goal>
78+
</goals>
79+
</execution>
80+
<execution>
81+
<id>test-compile</id>
82+
<phase>test-compile</phase>
83+
<goals>
84+
<goal>test-compile</goal>
85+
</goals>
86+
</execution>
87+
</executions>
88+
<dependencies>
89+
<dependency>
90+
<groupId>org.jetbrains.kotlin</groupId>
91+
<artifactId>kotlin-maven-allopen</artifactId>
92+
<version>${kotlin.version}</version>
93+
</dependency>
94+
</dependencies>
95+
</plugin>
96+
</plugins>
97+
</build>
98+
99+
<repositories>
100+
<repository>
101+
<id>spring-snapshots</id>
102+
<name>Spring Snapshots</name>
103+
<url>https://repo.spring.io/snapshot</url>
104+
<snapshots>
105+
<enabled>true</enabled>
106+
</snapshots>
107+
</repository>
108+
<repository>
109+
<id>spring-milestones</id>
110+
<name>Spring Milestones</name>
111+
<url>https://repo.spring.io/milestone</url>
112+
<snapshots>
113+
<enabled>false</enabled>
114+
</snapshots>
115+
</repository>
116+
</repositories>
117+
118+
<pluginRepositories>
119+
<pluginRepository>
120+
<id>spring-snapshots</id>
121+
<name>Spring Snapshots</name>
122+
<url>https://repo.spring.io/snapshot</url>
123+
<snapshots>
124+
<enabled>true</enabled>
125+
</snapshots>
126+
</pluginRepository>
127+
<pluginRepository>
128+
<id>spring-milestones</id>
129+
<name>Spring Milestones</name>
130+
<url>https://repo.spring.io/milestone</url>
131+
<snapshots>
132+
<enabled>false</enabled>
133+
</snapshots>
134+
</pluginRepository>
135+
</pluginRepositories>
136+
137+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.springbootkotlin
2+
3+
import org.springframework.beans.factory.annotation.Autowired
4+
import org.springframework.web.bind.annotation.GetMapping
5+
import org.springframework.web.bind.annotation.RestController
6+
7+
@RestController
8+
class HelloController @Autowired constructor(val helloService: HelloService) {
9+
10+
@GetMapping("/hello")
11+
fun helloKotlin(): String {
12+
return "hello world"
13+
}
14+
15+
@GetMapping("/hello-service")
16+
fun helloKotlinService(): String {
17+
return helloService.getHello()
18+
}
19+
20+
@GetMapping("/hello-poko")
21+
fun helloPoko(): HelloPoko {
22+
return HelloPoko("Hello from the poko")
23+
}
24+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.springbootkotlin
2+
3+
data class HelloPoko constructor(val greeting: String)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.springbootkotlin
2+
3+
import org.springframework.stereotype.Service
4+
5+
@Service
6+
class HelloService {
7+
fun getHello(): String {
8+
return "hello service"
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.springbootkotlin
2+
3+
import org.springframework.boot.SpringApplication
4+
import org.springframework.boot.autoconfigure.SpringBootApplication
5+
6+
@SpringBootApplication
7+
class KotlinDemoApplication
8+
9+
fun main(args: Array<String>) {
10+
SpringApplication.run(KotlinDemoApplication::class.java, *args)
11+
}

‎spring-boot-kotlin/src/main/resource/application.properties‎

Whitespace-only changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.kotlindemo
2+
3+
import com.baeldung.springbootkotlin.HelloPoko
4+
import com.baeldung.springbootkotlin.KotlinDemoApplication
5+
import org.junit.Assert
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
import org.springframework.beans.factory.annotation.Autowired
9+
import org.springframework.boot.test.context.SpringBootTest
10+
import org.springframework.boot.test.web.client.TestRestTemplate
11+
import org.springframework.http.HttpStatus
12+
import org.springframework.test.context.junit4.SpringRunner
13+
14+
@RunWith(SpringRunner::class)
15+
@SpringBootTest(classes = arrayOf(KotlinDemoApplication::class), webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
16+
class KotlinDemoApplicationTests {
17+
18+
@Autowired
19+
val testRestTemplate: TestRestTemplate? = null
20+
21+
@Test
22+
fun contextLoads() {
23+
}
24+
25+
@Test
26+
fun testHelloController() {
27+
var result = testRestTemplate?.getForEntity("/hello", String::class.java)
28+
29+
Assert.assertNotNull(result)
30+
Assert.assertEquals(result?.statusCode, HttpStatus.OK)
31+
Assert.assertEquals(result?.body, "hello world")
32+
}
33+
34+
@Test
35+
fun testHelloService() {
36+
var result = testRestTemplate?.getForEntity("/hello-service", String::class.java)
37+
38+
Assert.assertNotNull(result)
39+
Assert.assertEquals(result?.statusCode, HttpStatus.OK)
40+
Assert.assertEquals(result?.body, "hello service")
41+
}
42+
43+
@Test
44+
fun testHelloPoko() {
45+
var result = testRestTemplate?.getForEntity("/hello-poko", HelloPoko::class.java)
46+
47+
Assert.assertNotNull(result)
48+
Assert.assertEquals(result?.statusCode, HttpStatus.OK)
49+
Assert.assertEquals(result?.body, HelloPoko("Hello from the poko"))
50+
}
51+
52+
}

0 commit comments

Comments
 (0)