Create Kotlin Spring WebFlux module

This commit is contained in:
Andrey Shcherbakov
2018-07-14 21:24:33 +02:00
parent de2afd7da8
commit 08b0b8095a
9 changed files with 247 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package com.baeldung.springreactivekotlin
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class Application
fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.springreactivekotlin
import org.springframework.http.MediaType
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ResponseBody
import reactor.core.publisher.Flux
@Controller
class Controller {
@GetMapping(path = ["/numbers"], produces = [MediaType.APPLICATION_STREAM_JSON_VALUE])
@ResponseBody
fun getNumbers(): Flux<Int> {
return Flux.range(1, 100)
}
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.springreactivekotlin
class Device(val name: String, val reading: Double) {
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.springreactivekotlin
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.BodyInserters.fromObject
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import reactor.core.publisher.Mono
@Component
class HomeSensorsHandler {
var data = mapOf("lamp" to arrayOf(0.7, 0.65, 0.67), "fridge" to arrayOf(12.0, 11.9, 12.5))
fun setLight(request: ServerRequest): Mono<ServerResponse> = ServerResponse.ok().build()
fun getLightReading(request: ServerRequest): Mono<ServerResponse> =
ServerResponse.ok().body(fromObject(data["lamp"]!!))
fun getDeviceReadings(request: ServerRequest): Mono<ServerResponse> {
val id = request.pathVariable("id")
return ServerResponse.ok().body(fromObject(Device(id, 1.0)))
}
fun getAllDevices(request: ServerRequest): Mono<ServerResponse> =
ServerResponse.ok().body(fromObject(arrayOf("lamp", "tv")))
fun getAllDeviceApi(request: ServerRequest): Mono<ServerResponse> =
ServerResponse.ok().body(fromObject(arrayListOf("kettle", "fridge")))
fun setDeviceReadingApi(request: ServerRequest): Mono<ServerResponse> {
return request.bodyToMono(Device::class.java).flatMap { it ->
ServerResponse.ok().body(fromObject(Device(it.name.toUpperCase(), it.reading)))
}
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.springreactivekotlin
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.http.MediaType.TEXT_HTML
import org.springframework.web.reactive.function.server.router
@Configuration
class HomeSensorsRouters(private val handler: HomeSensorsHandler) {
@Bean
fun roomsRouter() = router {
(accept(TEXT_HTML) and "/room").nest {
GET("/light", handler::getLightReading)
POST("/light", handler::setLight)
}
}
@Bean
fun deviceRouter() = router {
accept(TEXT_HTML).nest {
(GET("/device/") or GET("/devices/")).invoke(handler::getAllDevices)
GET("/device/{id}", handler::getDeviceReadings)
}
(accept(APPLICATION_JSON) and "/api").nest {
(GET("/device/") or GET("/devices/")).invoke(handler::getAllDeviceApi)
POST("/device/", handler::setDeviceReadingApi)
}
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.springreactivekotlin
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.router
import org.springframework.web.reactive.function.BodyInserters.fromObject
@Configuration
class SimpleRoute {
@Bean
fun route() = router {
GET("/route") { _ -> ServerResponse.ok().body(fromObject(arrayOf(1, 2, 3))) }
}
}

View File

@@ -0,0 +1,35 @@
package veontomo
import com.baeldung.springreactivekotlin.SimpleRoute
import org.junit.Before
import org.junit.Test
import org.springframework.test.web.reactive.server.WebTestClient
class RoutesTest {
lateinit var client: WebTestClient
@Before
fun init() {
this.client = WebTestClient.bindToRouterFunction(SimpleRoute().route()).build()
}
@Test
fun whenRequestToRoute_thenStatusShouldBeOk() {
client.get()
.uri("/route")
.exchange()
.expectStatus().isOk
}
@Test
fun whenRequestToRoute_thenBodyShouldContainArray123() {
client.get()
.uri("/route")
.exchange()
.expectBody()
.json("[1, 2, 3]")
}
}