updating object with spring data mongodb and kotlin is not working

前端 未结 1 1233
借酒劲吻你
借酒劲吻你 2020-12-19 14:13

I have the following request handler

fun x(req: ServerRequest) = req.toMono()
    .flatMap {
        ...
        val oldest = myRepository.findOldest(...) //         


        
相关标签:
1条回答
  • 2020-12-19 15:19

    Nothing happens until someone subscribes to reactive Publisher. That's why it started to work when you used block(). If you need to make a call to DB and use the result in another DB request than use Mono / Flux operators like map(), flatMap(),... to build a pipeline of all the operations you need and after that return resulting Mono / Flux as controller’s response. Spring will subscribe to that Mono / Flux and will return the request. You don't need to block it. And it is not recommended to do it (to use block() method).

    Short example how to work with MongoDB reactive repositories in Java:

    @GetMapping("/users")
    public Mono<User> getPopulation() {
        return userRepository.findOldest()
                .flatMap(user -> {              // process the response from DB
                    user.setTheOldest(true);
                    return userRepository.save(user);
                })
                .map(user -> {...}); // another processing
    }
    
    0 讨论(0)
提交回复
热议问题