martypitt
02/27/2024, 5:28 PMkevin.cianfarini
02/27/2024, 5:30 PMMono
is conceptually equivalent to a suspending function which returns a single value.martypitt
02/27/2024, 5:31 PMkevin.cianfarini
02/27/2024, 5:32 PMmartypitt
02/27/2024, 5:34 PM@GetMapping("/api/customers")
fun getCustomer(id:String):Mono<Customer>
It returns exactly one Customer, so is serialized to JSON as { ... }
The equivalent on Flow would be:
@GetMapping("/api/customers")
fun getCustomer(id:String):Flow<Customer>
Which serializes as [ { ... } ]
, which isn't what we need to return.streetsofboston
02/27/2024, 5:40 PMkevin.cianfarini
02/27/2024, 5:41 PM@GetMapping("/api/customers")
fun getCustomer(id:String):Mono<Customer> = mono {
mySuspendingFunction()
}
kevin.cianfarini
02/27/2024, 5:42 PM@GetMapping
functions so you don’t have to use the mono builder everywherekevin.cianfarini
02/27/2024, 5:42 PMKotlin
section on these docs https://docs.spring.io/spring-framework/reference/web/webflux-functional.htmlkevin.cianfarini
02/27/2024, 5:43 PMval route = coRouter {
accept(APPLICATION_JSON).nest {
GET("/person/{id}", handler::getPerson)
GET("/person", handler::listPeople)
}
POST("/person", handler::createPerson)
}
class PersonHandler(private val repository: PersonRepository) {
// ...
suspend fun listPeople(request: ServerRequest): ServerResponse {
// ...
}
suspend fun createPerson(request: ServerRequest): ServerResponse {
// ...
}
suspend fun getPerson(request: ServerRequest): ServerResponse {
// ...
}
}
martypitt
02/27/2024, 5:49 PM