BAEL-755 Kotlin and Spring Boot 2.x tutorial
This commit is contained in:
parent
2bfc5bd979
commit
7bbfde57e1
1
pom.xml
1
pom.xml
|
@ -129,6 +129,7 @@
|
|||
<module>spring-apache-camel</module>
|
||||
<module>spring-batch</module>
|
||||
<module>spring-boot</module>
|
||||
<module>spring-boot-kotlin</module>
|
||||
<module>spring-cloud-data-flow</module>
|
||||
<module>spring-cloud</module>
|
||||
<module>spring-core</module>
|
||||
|
|
|
@ -0,0 +1,137 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-boot-kotlin</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<kotlin.version>1.1.2</kotlin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jre8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-reflect</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-kotlin</artifactId>
|
||||
<version>2.8.7</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
|
||||
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<version>${kotlin.version}</version>
|
||||
<configuration>
|
||||
<compilerPlugins>
|
||||
<plugin>spring</plugin>
|
||||
</compilerPlugins>
|
||||
<jvmTarget>1.8</jvmTarget>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>test-compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-allopen</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
</project>
|
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
package com.baeldung.springbootkotlin
|
||||
|
||||
data class HelloPoko constructor(val greeting: String)
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.springbootkotlin
|
||||
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class HelloService {
|
||||
fun getHello(): String {
|
||||
return "hello service"
|
||||
}
|
||||
}
|
|
@ -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<String>) {
|
||||
SpringApplication.run(KotlinDemoApplication::class.java, *args)
|
||||
}
|
|
@ -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"))
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue