Flatmap 在展平嵌套 Mono 时抛出编译时错误

发布于 2025-01-11 21:15:23 字数 2738 浏览 0 评论 0原文

有下面的方法

private Mono<EventSlotBook> getTestEventSlotBook(EventUserAppt eventUserAppt){
    Query query = new Query();
    query.addCriteria(
            new Criteria().andOperator(
                    Criteria.where("eventId").is(eventUserAppt.getEventId()),
                    Criteria.where("eventConfigId").is(eventUserAppt.getEventConfigId()),
                    Criteria.where("eventSlotId").is(eventUserAppt.getEventSlotId()),
                    Criteria.where("appointmentDate").in(eventUserAppt.getAppointmentDate()
                    )));

    return this.reactiveMongoTemplate.findOne(query, EventSlotBook.class)
            .flatMap(eventSlotExistingEntity -> {
                if(eventSlotExistingEntity.getEventUsers() != null) {
                    eventSlotExistingEntity.getEventUsers().add(eventUserAppt.getEventUser());
                }
                return Mono.just(eventSlotExistingEntity);
            })
            .switchIfEmpty(getInitialTestEventSlotBook(eventUserAppt));
}

和上面的方法由

public Mono<EventSlotBookRequestDto> saveEventSlotBookFinal(Mono<EventSlotBookRequestDto> eventSlotBookRequestDtoMono){
    log.info("Start::SaveEventSlotBook#######Final");
    Mono<EventSlotBookRequestDto> eventDtoSaved =
            eventSlotBookRequestDtoMono.map(AppUtils::dtoToEventUserApptEntity)
                    .flatMap(eventUserApptEntity -> getEventUserAppt(eventUserApptEntity))
                    .doOnNext(eventUserApptBeforeSave -> {
                        log.info("@@@@BeforeSave::{}",eventUserApptBeforeSave);
                    })
                    .flatMap(eventUserApptRepository::save)
                    .doOnNext( eventUserApptAftereSave -> {
                        log.info("@@@@AfterSave::{}",eventUserApptAftereSave);
                    })

                    .map(eventUserApptAfterSave -> getTestEventSlotBook(eventUserApptAfterSave)) -> IDE shows it returns Mono<Mono<EventSlotBoo>>
                    .flatMap(eventSlotBookrepository::save) --> Compile time error: o instance(s) of type variable(s) exist so that Mono<EventSlotBook> conforms to EventSlotBook
                            .map(eventSlotBooEntity -> AppUtils.entityToDto((EventSlotBook)eventSlotBooEntity));



    log.info("End::SaveEventSlotBook#####Final");
    return eventDtoSaved;
}

@Repository 调用 公共接口 EventSlotBookRepository 扩展了 ReactiveMongoRepository{ >

不知道为什么 .flatMap(eventSlotBookrepository::save) -- 编译时错误:o 类型变量的实例存在,因此 Mono 符合 EventSlotBook 它会抛出此错误。 flatMap 预期扁平化 Mono到 EventSlotBook 并保存此数据

Have below Method

private Mono<EventSlotBook> getTestEventSlotBook(EventUserAppt eventUserAppt){
    Query query = new Query();
    query.addCriteria(
            new Criteria().andOperator(
                    Criteria.where("eventId").is(eventUserAppt.getEventId()),
                    Criteria.where("eventConfigId").is(eventUserAppt.getEventConfigId()),
                    Criteria.where("eventSlotId").is(eventUserAppt.getEventSlotId()),
                    Criteria.where("appointmentDate").in(eventUserAppt.getAppointmentDate()
                    )));

    return this.reactiveMongoTemplate.findOne(query, EventSlotBook.class)
            .flatMap(eventSlotExistingEntity -> {
                if(eventSlotExistingEntity.getEventUsers() != null) {
                    eventSlotExistingEntity.getEventUsers().add(eventUserAppt.getEventUser());
                }
                return Mono.just(eventSlotExistingEntity);
            })
            .switchIfEmpty(getInitialTestEventSlotBook(eventUserAppt));
}

And above method called by

public Mono<EventSlotBookRequestDto> saveEventSlotBookFinal(Mono<EventSlotBookRequestDto> eventSlotBookRequestDtoMono){
    log.info("Start::SaveEventSlotBook#######Final");
    Mono<EventSlotBookRequestDto> eventDtoSaved =
            eventSlotBookRequestDtoMono.map(AppUtils::dtoToEventUserApptEntity)
                    .flatMap(eventUserApptEntity -> getEventUserAppt(eventUserApptEntity))
                    .doOnNext(eventUserApptBeforeSave -> {
                        log.info("@@@@BeforeSave::{}",eventUserApptBeforeSave);
                    })
                    .flatMap(eventUserApptRepository::save)
                    .doOnNext( eventUserApptAftereSave -> {
                        log.info("@@@@AfterSave::{}",eventUserApptAftereSave);
                    })

                    .map(eventUserApptAfterSave -> getTestEventSlotBook(eventUserApptAfterSave)) -> IDE shows it returns Mono<Mono<EventSlotBoo>>
                    .flatMap(eventSlotBookrepository::save) --> Compile time error: o instance(s) of type variable(s) exist so that Mono<EventSlotBook> conforms to EventSlotBook
                            .map(eventSlotBooEntity -> AppUtils.entityToDto((EventSlotBook)eventSlotBooEntity));



    log.info("End::SaveEventSlotBook#####Final");
    return eventDtoSaved;
}

@Repository
public interface EventSlotBookRepository extends ReactiveMongoRepository<EventSlotBook,String> {
}

Not sure why .flatMap(eventSlotBookrepository::save) --> Compile time error: o instance(s) of type variable(s) exist so that Mono conforms to EventSlotBook it throws this error. flatMap expected flattened Mono<Mono> to EventSlotBook and save this data

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀里藏娇 2025-01-18 21:15:23

ReactiveMongoRepository 没有可以接受 Monosave 方法。它只能接受实体类型的实例,因此以下内容可以工作:

.flatMap(eventUserApptAfterSave -> getTestEventSlotBook(eventUserApptAfterSave)) // map changed to flatMap
.flatMap(eventSlotBookrepository::save)

ReactiveMongoRepository does not have a save method which would accept a Mono. It can only accept an instance of the entity type, so the following would work:

.flatMap(eventUserApptAfterSave -> getTestEventSlotBook(eventUserApptAfterSave)) // map changed to flatMap
.flatMap(eventSlotBookrepository::save)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文