Why Spring ReactiveMongoRepository does't have save method for Mono?

北慕城南 提交于 2019-12-09 03:22:32

问题


I have a MovieRepository which extended ReactiveMongoRepository. I want to save a single POJO in a reactive way. But ReactiveMongoRepository doesn't provide save method for Mono or Publisher. I have to use block() method or use the saveAll method in the ReactiveMongoRepository.

public Mono<ServerResponse> create(ServerRequest request) {

    Mono<Movie> movieMono = request.bodyToMono(Movie.class);
    return movieRepository.save(movieMono.block()) //
            .flatMap((movie) -> ServerResponse.ok().body(fromObject(movie)));
}

Is there a better way to solve this kind of problem? I don't think use block method is a good idea for reactive programming.


回答1:


You could do something like this

 Mono<Movie> movieMonoSaved = movieMono.flatMap(movieRepository::save);
 return ServerResponse.status(HttpStatus.CREATED).body(movieMonoSaved, Movie.class);


来源:https://stackoverflow.com/questions/47918441/why-spring-reactivemongorepository-doest-have-save-method-for-mono

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!