Skip to content

Commit 494a9c1

Browse files
Merge pull request eugenp#1981 from eugenp/spring-boot-kotlin
BAEL-755 - moving kotlin code to spring-5-mvc
2 parents beef36b + ec435b1 commit 494a9c1

6 files changed

Lines changed: 178 additions & 18 deletions

File tree

‎spring-5-mvc/pom.xml‎

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>com.baeldung</groupId>
@@ -15,7 +15,8 @@
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
1717
<version>2.0.0.M1</version>
18-
<relativePath /> <!-- lookup parent from repository -->
18+
<relativePath/>
19+
<!-- lookup parent from repository -->
1920
</parent>
2021

2122
<dependencies>
@@ -35,20 +36,37 @@
3536
<groupId>org.springframework.boot</groupId>
3637
<artifactId>spring-boot-starter-web</artifactId>
3738
</dependency>
38-
39+
3940

4041
<!-- utils -->
4142
<dependency>
4243
<groupId>org.apache.commons</groupId>
43-
<artifactId>commons-lang3</artifactId>
44+
<artifactId>commons-lang3</artifactId>
4445
</dependency>
4546
<dependency>
4647
<groupId>org.slf4j</groupId>
4748
<artifactId>slf4j-api</artifactId>
4849
</dependency>
4950
<dependency>
5051
<groupId>org.slf4j</groupId>
51-
<artifactId>jcl-over-slf4j</artifactId>
52+
<artifactId>jcl-over-slf4j</artifactId>
53+
</dependency>
54+
55+
<!--kotlin deps-->
56+
<dependency>
57+
<groupId>org.jetbrains.kotlin</groupId>
58+
<artifactId>kotlin-stdlib-jre8</artifactId>
59+
<version>${kotlin.version}</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.jetbrains.kotlin</groupId>
63+
<artifactId>kotlin-reflect</artifactId>
64+
<version>${kotlin.version}</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.fasterxml.jackson.module</groupId>
68+
<artifactId>jackson-module-kotlin</artifactId>
69+
<version>2.8.7</version>
5270
</dependency>
5371

5472
<!-- runtime and test scoped -->
@@ -74,12 +92,12 @@
7492
<scope>test</scope>
7593
</dependency>
7694

77-
<dependency>
95+
<dependency>
7896
<groupId>junit</groupId>
7997
<artifactId>junit</artifactId>
8098
<scope>test</scope>
8199
</dependency>
82-
100+
83101
<dependency>
84102
<groupId>com.jayway.restassured</groupId>
85103
<artifactId>rest-assured</artifactId>
@@ -90,18 +108,58 @@
90108
</dependencies>
91109

92110
<build>
93-
<plugins>
111+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
112+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
113+
<plugins>
114+
<plugin>
115+
<groupId>org.springframework.boot</groupId>
116+
<artifactId>spring-boot-maven-plugin</artifactId>
117+
</plugin>
118+
<plugin>
119+
<artifactId>kotlin-maven-plugin</artifactId>
120+
<groupId>org.jetbrains.kotlin</groupId>
121+
<version>${kotlin.version}</version>
122+
<configuration>
123+
<compilerPlugins>
124+
<plugin>spring</plugin>
125+
</compilerPlugins>
126+
<jvmTarget>1.8</jvmTarget>
127+
</configuration>
128+
<executions>
129+
<execution>
130+
<id>compile</id>
131+
<phase>compile</phase>
132+
<goals>
133+
<goal>compile</goal>
134+
</goals>
135+
</execution>
136+
<execution>
137+
<id>test-compile</id>
138+
<phase>test-compile</phase>
139+
<goals>
140+
<goal>test-compile</goal>
141+
</goals>
142+
</execution>
143+
</executions>
144+
<dependencies>
145+
<dependency>
146+
<groupId>org.jetbrains.kotlin</groupId>
147+
<artifactId>kotlin-maven-allopen</artifactId>
148+
<version>${kotlin.version}</version>
149+
</dependency>
150+
</dependencies>
151+
</plugin>
94152
<plugin>
95153
<groupId>org.apache.maven.plugins</groupId>
96-
<artifactId>maven-surefire-plugin</artifactId>
154+
<artifactId>maven-surefire-plugin</artifactId>
97155
<configuration>
98-
<testFailureIgnore>true</testFailureIgnore>
99-
<failIfNoTests>false</failIfNoTests>
100-
<excludes>
101-
<exclude>**/*IntegrationTest.java</exclude>
102-
<exclude>**/*LiveTest.java</exclude>
103-
</excludes>
104-
</configuration>
156+
<testFailureIgnore>true</testFailureIgnore>
157+
<failIfNoTests>false</failIfNoTests>
158+
<excludes>
159+
<exclude>**/*IntegrationTest.java</exclude>
160+
<exclude>**/*LiveTest.java</exclude>
161+
</excludes>
162+
</configuration>
105163
</plugin>
106164

107165
</plugins>
@@ -145,10 +203,11 @@
145203
</pluginRepositories>
146204

147205
<properties>
148-
<rest-assured.version>2.9.0</rest-assured.version>
206+
<rest-assured.version>2.9.0</rest-assured.version>
149207
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
150208
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
151-
<java.version>1.8</java.version>
209+
<java.version>1.8</java.version>
210+
<kotlin.version>1.1.2</kotlin.version>
152211
</properties>
153212

154213
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.springbootkotlin
2+
3+
import org.springframework.web.bind.annotation.GetMapping
4+
import org.springframework.web.bind.annotation.RestController
5+
6+
@RestController
7+
class HelloController(val helloService: HelloService) {
8+
9+
@GetMapping("/hello")
10+
fun helloKotlin(): String {
11+
return "hello world"
12+
}
13+
14+
@GetMapping("/hello-service")
15+
fun helloKotlinService(): String {
16+
return helloService.getHello()
17+
}
18+
19+
@GetMapping("/hello-dto")
20+
fun helloDto(): HelloDto {
21+
return HelloDto("Hello from the dto")
22+
}
23+
}
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 HelloDto(val greeting: String)
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.stereotype.Service
4+
5+
@Service
6+
class HelloService {
7+
8+
fun getHello(): String {
9+
return "hello service"
10+
}
11+
}
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package springbootkotlin
2+
3+
import com.baeldung.springbootkotlin.HelloDto
4+
import com.baeldung.springbootkotlin.KotlinDemoApplication
5+
import org.junit.Assert.assertEquals
6+
import org.junit.Assert.assertNotNull
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import org.springframework.beans.factory.annotation.Autowired
10+
import org.springframework.boot.test.context.SpringBootTest
11+
import org.springframework.boot.test.web.client.TestRestTemplate
12+
import org.springframework.http.HttpStatus
13+
import org.springframework.test.context.junit4.SpringRunner
14+
15+
@RunWith(SpringRunner::class)
16+
@SpringBootTest(classes = arrayOf(KotlinDemoApplication::class), webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
17+
class KotlinDemoApplicationIntegrationTest {
18+
19+
@Autowired
20+
val testRestTemplate: TestRestTemplate? = null
21+
22+
@Test
23+
fun contextLoads() {
24+
}
25+
26+
@Test
27+
fun testHelloController() {
28+
val result = testRestTemplate?.getForEntity("/hello", String::class.java)
29+
30+
assertNotNull(result)
31+
assertEquals(result?.statusCode, HttpStatus.OK)
32+
assertEquals(result?.body, "hello world")
33+
}
34+
35+
@Test
36+
fun testHelloService() {
37+
val result = testRestTemplate?.getForEntity("/hello-service", String::class.java)
38+
39+
assertNotNull(result)
40+
assertEquals(result?.statusCode, HttpStatus.OK)
41+
assertEquals(result?.body, "hello service")
42+
}
43+
44+
@Test
45+
fun testHelloDto() {
46+
val result = testRestTemplate?.getForEntity("/hello-dto", HelloDto::class.java)
47+
48+
assertNotNull(result)
49+
assertEquals(result?.statusCode, HttpStatus.OK)
50+
assertEquals(result?.body, HelloDto("Hello from the dto"))
51+
}
52+
53+
}

0 commit comments

Comments
 (0)