From 7bbfde57e16f7a3de1e7e59ca0ddbbb571b5c1ca Mon Sep 17 00:00:00 2001 From: tschiman Date: Fri, 26 May 2017 18:04:48 -0600 Subject: [PATCH] BAEL-755 Kotlin and Spring Boot 2.x tutorial --- pom.xml | 1 + spring-boot-kotlin/pom.xml | 137 ++++++++++++++++++ .../springbootkotlin/HelloController.kt | 24 +++ .../baeldung/springbootkotlin/HelloPoko.kt | 3 + .../baeldung/springbootkotlin/HelloService.kt | 10 ++ .../springbootkotlin/KotlinDemoApplication.kt | 11 ++ .../src/main/resource/application.properties | 0 .../KotlinDemoApplicationTests.kt | 52 +++++++ 8 files changed, 238 insertions(+) create mode 100644 spring-boot-kotlin/pom.xml create mode 100644 spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloController.kt create mode 100644 spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloPoko.kt create mode 100644 spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloService.kt create mode 100644 spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/KotlinDemoApplication.kt create mode 100644 spring-boot-kotlin/src/main/resource/application.properties create mode 100644 spring-boot-kotlin/src/test/kotlin/springbootkotlin/KotlinDemoApplicationTests.kt diff --git a/pom.xml b/pom.xml index 3663d1308a..d54ac5a2b8 100644 --- a/pom.xml +++ b/pom.xml @@ -129,6 +129,7 @@ spring-apache-camel spring-batch spring-boot + spring-boot-kotlin spring-cloud-data-flow spring-cloud spring-core diff --git a/spring-boot-kotlin/pom.xml b/spring-boot-kotlin/pom.xml new file mode 100644 index 0000000000..c882daa816 --- /dev/null +++ b/spring-boot-kotlin/pom.xml @@ -0,0 +1,137 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.BUILD-SNAPSHOT + + + +spring-boot-kotlin +1.0.0-SNAPSHOT + + + true + UTF-8 + UTF-8 + 1.8 + 1.1.2 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.jetbrains.kotlin + kotlin-stdlib-jre8 + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-reflect + ${kotlin.version} + + + com.fasterxml.jackson.module + jackson-module-kotlin + 2.8.7 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin + + + org.springframework.boot + spring-boot-maven-plugin + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin.version} + + + spring + + 1.8 + + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + + org.jetbrains.kotlin + kotlin-maven-allopen + ${kotlin.version} + + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + \ No newline at end of file diff --git a/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloController.kt b/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloController.kt new file mode 100644 index 0000000000..622271211b --- /dev/null +++ b/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloController.kt @@ -0,0 +1,24 @@ +package com.baeldung.springbootkotlin + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +class HelloController @Autowired constructor(val helloService: HelloService) { + + @GetMapping("/hello") + fun helloKotlin(): String { + return "hello world" + } + + @GetMapping("/hello-service") + fun helloKotlinService(): String { + return helloService.getHello() + } + + @GetMapping("/hello-poko") + fun helloPoko(): HelloPoko { + return HelloPoko("Hello from the poko") + } +} \ No newline at end of file diff --git a/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloPoko.kt b/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloPoko.kt new file mode 100644 index 0000000000..7e9c50e073 --- /dev/null +++ b/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloPoko.kt @@ -0,0 +1,3 @@ +package com.baeldung.springbootkotlin + +data class HelloPoko constructor(val greeting: String) diff --git a/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloService.kt b/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloService.kt new file mode 100644 index 0000000000..c988b64e05 --- /dev/null +++ b/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/HelloService.kt @@ -0,0 +1,10 @@ +package com.baeldung.springbootkotlin + +import org.springframework.stereotype.Service + +@Service +class HelloService { + fun getHello(): String { + return "hello service" + } +} \ No newline at end of file diff --git a/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/KotlinDemoApplication.kt b/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/KotlinDemoApplication.kt new file mode 100644 index 0000000000..dbe5694b6d --- /dev/null +++ b/spring-boot-kotlin/src/main/kotlin/com/baeldung/springbootkotlin/KotlinDemoApplication.kt @@ -0,0 +1,11 @@ +package com.baeldung.springbootkotlin + +import org.springframework.boot.SpringApplication +import org.springframework.boot.autoconfigure.SpringBootApplication + +@SpringBootApplication +class KotlinDemoApplication + +fun main(args: Array) { + SpringApplication.run(KotlinDemoApplication::class.java, *args) +} diff --git a/spring-boot-kotlin/src/main/resource/application.properties b/spring-boot-kotlin/src/main/resource/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-boot-kotlin/src/test/kotlin/springbootkotlin/KotlinDemoApplicationTests.kt b/spring-boot-kotlin/src/test/kotlin/springbootkotlin/KotlinDemoApplicationTests.kt new file mode 100644 index 0000000000..c6d9146edd --- /dev/null +++ b/spring-boot-kotlin/src/test/kotlin/springbootkotlin/KotlinDemoApplicationTests.kt @@ -0,0 +1,52 @@ +package com.example.kotlindemo + +import com.baeldung.springbootkotlin.HelloPoko +import com.baeldung.springbootkotlin.KotlinDemoApplication +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.web.client.TestRestTemplate +import org.springframework.http.HttpStatus +import org.springframework.test.context.junit4.SpringRunner + +@RunWith(SpringRunner::class) +@SpringBootTest(classes = arrayOf(KotlinDemoApplication::class), webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +class KotlinDemoApplicationTests { + + @Autowired + val testRestTemplate: TestRestTemplate? = null + + @Test + fun contextLoads() { + } + + @Test + fun testHelloController() { + var result = testRestTemplate?.getForEntity("/hello", String::class.java) + + Assert.assertNotNull(result) + Assert.assertEquals(result?.statusCode, HttpStatus.OK) + Assert.assertEquals(result?.body, "hello world") + } + + @Test + fun testHelloService() { + var result = testRestTemplate?.getForEntity("/hello-service", String::class.java) + + Assert.assertNotNull(result) + Assert.assertEquals(result?.statusCode, HttpStatus.OK) + Assert.assertEquals(result?.body, "hello service") + } + + @Test + fun testHelloPoko() { + var result = testRestTemplate?.getForEntity("/hello-poko", HelloPoko::class.java) + + Assert.assertNotNull(result) + Assert.assertEquals(result?.statusCode, HttpStatus.OK) + Assert.assertEquals(result?.body, HelloPoko("Hello from the poko")) + } + +}