Merge pull request #1981 from eugenp/spring-boot-kotlin

BAEL-755 - moving kotlin code to spring-5-mvc
This commit is contained in:
slavisa-baeldung 2017-06-03 14:35:17 +01:00 committed by GitHub
commit 494a9c1af1
6 changed files with 178 additions and 18 deletions

View File

@ -1,6 +1,6 @@
<?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">
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>
<groupId>com.baeldung</groupId>
@ -15,7 +15,8 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M1</version>
<relativePath /> <!-- lookup parent from repository -->
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<dependencies>
@ -35,12 +36,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- utils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
@ -48,7 +49,24 @@
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<!--kotlin deps-->
<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>
<!-- runtime and test scoped -->
@ -74,12 +92,12 @@
<scope>test</scope>
</dependency>
<dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
@ -90,18 +108,58 @@
</dependencies>
<build>
<plugins>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<failIfNoTests>false</failIfNoTests>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
<testFailureIgnore>true</testFailureIgnore>
<failIfNoTests>false</failIfNoTests>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
@ -145,10 +203,11 @@
</pluginRepositories>
<properties>
<rest-assured.version>2.9.0</rest-assured.version>
<rest-assured.version>2.9.0</rest-assured.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<java.version>1.8</java.version>
<kotlin.version>1.1.2</kotlin.version>
</properties>
</project>

View File

@ -0,0 +1,23 @@
package com.baeldung.springbootkotlin
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class HelloController(val helloService: HelloService) {
@GetMapping("/hello")
fun helloKotlin(): String {
return "hello world"
}
@GetMapping("/hello-service")
fun helloKotlinService(): String {
return helloService.getHello()
}
@GetMapping("/hello-dto")
fun helloDto(): HelloDto {
return HelloDto("Hello from the dto")
}
}

View File

@ -0,0 +1,3 @@
package com.baeldung.springbootkotlin
data class HelloDto(val greeting: String)

View File

@ -0,0 +1,11 @@
package com.baeldung.springbootkotlin
import org.springframework.stereotype.Service
@Service
class HelloService {
fun getHello(): String {
return "hello service"
}
}

View File

@ -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)
}

View File

@ -0,0 +1,53 @@
package springbootkotlin
import com.baeldung.springbootkotlin.HelloDto
import com.baeldung.springbootkotlin.KotlinDemoApplication
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
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 KotlinDemoApplicationIntegrationTest {
@Autowired
val testRestTemplate: TestRestTemplate? = null
@Test
fun contextLoads() {
}
@Test
fun testHelloController() {
val result = testRestTemplate?.getForEntity("/hello", String::class.java)
assertNotNull(result)
assertEquals(result?.statusCode, HttpStatus.OK)
assertEquals(result?.body, "hello world")
}
@Test
fun testHelloService() {
val result = testRestTemplate?.getForEntity("/hello-service", String::class.java)
assertNotNull(result)
assertEquals(result?.statusCode, HttpStatus.OK)
assertEquals(result?.body, "hello service")
}
@Test
fun testHelloDto() {
val result = testRestTemplate?.getForEntity("/hello-dto", HelloDto::class.java)
assertNotNull(result)
assertEquals(result?.statusCode, HttpStatus.OK)
assertEquals(result?.body, HelloDto("Hello from the dto"))
}
}