Skip to content

Commit 08b0b80

Browse files
committed
Create Kotlin Spring WebFlux module
1 parent de2afd7 commit 08b0b80

9 files changed

Lines changed: 247 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
<module>spring-mvc-kotlin</module>
195195
<module>spring-protobuf</module>
196196
<module>spring-quartz</module>
197+
<module>spring-reactive-kotlin</module>
197198
<module>spring-rest-angular</module>
198199
<module>spring-rest-full</module>
199200
<module>spring-rest-query-language</module>

‎spring-reactive-kotlin/pom.xml‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>springreactivekotlin</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springreactivekotlin</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.3.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<kotlin.version>1.2.41</kotlin.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-webflux</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.fasterxml.jackson.module</groupId>
39+
<artifactId>jackson-module-kotlin</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.jetbrains.kotlin</groupId>
43+
<artifactId>kotlin-stdlib-jdk8</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.jetbrains.kotlin</groupId>
47+
<artifactId>kotlin-reflect</artifactId>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-test</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>io.projectreactor</groupId>
57+
<artifactId>reactor-test</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
</dependencies>
61+
62+
<build>
63+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
64+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-maven-plugin</artifactId>
69+
</plugin>
70+
<plugin>
71+
<artifactId>kotlin-maven-plugin</artifactId>
72+
<groupId>org.jetbrains.kotlin</groupId>
73+
<configuration>
74+
<args>
75+
<arg>-Xjsr305=strict</arg>
76+
</args>
77+
<compilerPlugins>
78+
<plugin>spring</plugin>
79+
</compilerPlugins>
80+
</configuration>
81+
<dependencies>
82+
<dependency>
83+
<groupId>org.jetbrains.kotlin</groupId>
84+
<artifactId>kotlin-maven-allopen</artifactId>
85+
<version>${kotlin.version}</version>
86+
</dependency>
87+
</dependencies>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
92+
93+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.springreactivekotlin
2+
3+
import org.springframework.boot.SpringApplication
4+
import org.springframework.boot.autoconfigure.SpringBootApplication
5+
6+
@SpringBootApplication
7+
class Application
8+
9+
fun main(args: Array<String>) {
10+
SpringApplication.run(Application::class.java, *args)
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.springreactivekotlin
2+
3+
import org.springframework.http.MediaType
4+
import org.springframework.stereotype.Controller
5+
import org.springframework.web.bind.annotation.GetMapping
6+
import org.springframework.web.bind.annotation.ResponseBody
7+
import reactor.core.publisher.Flux
8+
9+
@Controller
10+
class Controller {
11+
12+
@GetMapping(path = ["/numbers"], produces = [MediaType.APPLICATION_STREAM_JSON_VALUE])
13+
@ResponseBody
14+
fun getNumbers(): Flux<Int> {
15+
return Flux.range(1, 100)
16+
}
17+
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.springreactivekotlin
2+
3+
class Device(val name: String, val reading: Double) {
4+
5+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.springreactivekotlin
2+
3+
import org.springframework.stereotype.Component
4+
import org.springframework.web.reactive.function.BodyInserters.fromObject
5+
import org.springframework.web.reactive.function.server.ServerRequest
6+
import org.springframework.web.reactive.function.server.ServerResponse
7+
import reactor.core.publisher.Mono
8+
9+
@Component
10+
class HomeSensorsHandler {
11+
12+
var data = mapOf("lamp" to arrayOf(0.7, 0.65, 0.67), "fridge" to arrayOf(12.0, 11.9, 12.5))
13+
14+
fun setLight(request: ServerRequest): Mono<ServerResponse> = ServerResponse.ok().build()
15+
16+
fun getLightReading(request: ServerRequest): Mono<ServerResponse> =
17+
ServerResponse.ok().body(fromObject(data["lamp"]!!))
18+
19+
fun getDeviceReadings(request: ServerRequest): Mono<ServerResponse> {
20+
val id = request.pathVariable("id")
21+
return ServerResponse.ok().body(fromObject(Device(id, 1.0)))
22+
}
23+
24+
fun getAllDevices(request: ServerRequest): Mono<ServerResponse> =
25+
ServerResponse.ok().body(fromObject(arrayOf("lamp", "tv")))
26+
27+
fun getAllDeviceApi(request: ServerRequest): Mono<ServerResponse> =
28+
ServerResponse.ok().body(fromObject(arrayListOf("kettle", "fridge")))
29+
30+
fun setDeviceReadingApi(request: ServerRequest): Mono<ServerResponse> {
31+
return request.bodyToMono(Device::class.java).flatMap { it ->
32+
ServerResponse.ok().body(fromObject(Device(it.name.toUpperCase(), it.reading)))
33+
}
34+
}
35+
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.springreactivekotlin
2+
3+
import org.springframework.context.annotation.Bean
4+
import org.springframework.context.annotation.Configuration
5+
import org.springframework.http.MediaType.APPLICATION_JSON
6+
import org.springframework.http.MediaType.TEXT_HTML
7+
import org.springframework.web.reactive.function.server.router
8+
9+
@Configuration
10+
class HomeSensorsRouters(private val handler: HomeSensorsHandler) {
11+
12+
@Bean
13+
fun roomsRouter() = router {
14+
(accept(TEXT_HTML) and "/room").nest {
15+
GET("/light", handler::getLightReading)
16+
POST("/light", handler::setLight)
17+
}
18+
}
19+
20+
@Bean
21+
fun deviceRouter() = router {
22+
accept(TEXT_HTML).nest {
23+
(GET("/device/") or GET("/devices/")).invoke(handler::getAllDevices)
24+
GET("/device/{id}", handler::getDeviceReadings)
25+
}
26+
(accept(APPLICATION_JSON) and "/api").nest {
27+
(GET("/device/") or GET("/devices/")).invoke(handler::getAllDeviceApi)
28+
POST("/device/", handler::setDeviceReadingApi)
29+
}
30+
}
31+
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.springreactivekotlin
2+
3+
import org.springframework.context.annotation.Bean
4+
import org.springframework.context.annotation.Configuration
5+
import org.springframework.web.reactive.function.server.ServerResponse
6+
import org.springframework.web.reactive.function.server.router
7+
8+
import org.springframework.web.reactive.function.BodyInserters.fromObject
9+
10+
@Configuration
11+
class SimpleRoute {
12+
@Bean
13+
fun route() = router {
14+
GET("/route") { _ -> ServerResponse.ok().body(fromObject(arrayOf(1, 2, 3))) }
15+
}
16+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package veontomo
2+
3+
import com.baeldung.springreactivekotlin.SimpleRoute
4+
import org.junit.Before
5+
import org.junit.Test
6+
import org.springframework.test.web.reactive.server.WebTestClient
7+
8+
class RoutesTest {
9+
10+
lateinit var client: WebTestClient
11+
12+
@Before
13+
fun init() {
14+
this.client = WebTestClient.bindToRouterFunction(SimpleRoute().route()).build()
15+
}
16+
17+
18+
@Test
19+
fun whenRequestToRoute_thenStatusShouldBeOk() {
20+
client.get()
21+
.uri("/route")
22+
.exchange()
23+
.expectStatus().isOk
24+
}
25+
26+
27+
@Test
28+
fun whenRequestToRoute_thenBodyShouldContainArray123() {
29+
client.get()
30+
.uri("/route")
31+
.exchange()
32+
.expectBody()
33+
.json("[1, 2, 3]")
34+
}
35+
}

0 commit comments

Comments
 (0)