You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.6 KiB
61 lines
1.6 KiB
package com.example.demo.controller
|
|
|
|
import com.example.demo.model.User
|
|
import com.example.demo.service.UserService
|
|
import kotlinx.coroutines.*
|
|
import kotlinx.coroutines.flow.Flow
|
|
import kotlinx.coroutines.flow.toList
|
|
import org.springframework.beans.factory.annotation.Autowired
|
|
import org.springframework.web.bind.annotation.GetMapping
|
|
import org.springframework.web.bind.annotation.PathVariable
|
|
import org.springframework.web.bind.annotation.RestController
|
|
import reactor.core.publisher.Flux
|
|
import reactor.core.publisher.Mono
|
|
import org.springframework.context.annotation.Lazy
|
|
|
|
|
|
@RestController
|
|
@Lazy
|
|
class HomeController(@Autowired @Lazy private val userService: UserService) {
|
|
@GetMapping("/")
|
|
fun index(): Mono<String> {
|
|
return Mono.just("foo")
|
|
}
|
|
|
|
@GetMapping("/data")
|
|
fun dataHandler(): Mono<MyData> {
|
|
return Mono.just(MyData(1, "hello2"))
|
|
}
|
|
|
|
@GetMapping("/data2")
|
|
suspend fun dataHandler2(): MyData = withContext(Dispatchers.IO) {
|
|
delay(10_000)
|
|
MyData(1, "hello3")
|
|
}
|
|
|
|
@GetMapping("/users")
|
|
fun users(): Flux<User> {
|
|
return userService.users()
|
|
}
|
|
|
|
@GetMapping("/users2")
|
|
suspend fun users2(): Flow<User> {
|
|
return userService.users2()
|
|
}
|
|
|
|
@GetMapping("/users3")
|
|
suspend fun users3() = coroutineScope {
|
|
val fun1 = async {
|
|
userService.users2().toList()
|
|
}
|
|
|
|
Pair(fun1.await(), 1)
|
|
}
|
|
|
|
@GetMapping("/messages/{userName}")
|
|
suspend fun messages(@PathVariable userName: String) = coroutineScope {
|
|
userService.getUserMessages(userName)
|
|
}
|
|
}
|
|
|
|
data class MyData(val id: Int, val name2: String)
|