I am trying to migrate some project from Spring Reactor to kotlin coroutines. I have some controller based on spring webflux like that:
@RestController
class Controller(val productRepository: ProductsRepository) {
@GetMapping("/product")
fun find(@RequestParam id: String): Mono<Product> {
return productRepository.findById(id)
}
}
This controller uses reactive spring data repository:
@Repository
interface ProductsRepository : ReactiveMongoRepository<Product, String>
According to this official documentation - https://docs.spring.io/spring/docs/5.2.0.M1/spring-framework-reference/languages.html#how-reactive-translates-to-coroutines, my function find
in controller should be translated to suspend fun
and this function should return an instance of Product class instead of reactive Mono wrapper of Product. Something like that:
@RestController
class Controller(val productRepository: ProductsRepository) {
@GetMapping("/product")
suspend fun find(@RequestParam id: String): Product {
return productRepository.findById(id)
}
}
But my productRepository deals with Mono and Flux, not suspended functions. How should I use spring data abstraction properly in that case?