Flatmap 在展平嵌套 Mono 时抛出编译时错误
Flatmap throws compile time error while flattened nested Mono
有以下方法
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;
}
@存储库
public interface EventSlotBookRepository extends ReactiveMongoRepository {
}
不确定为什么 .flatMap(eventSlotBookrepository::save) --> 编译时错误:o 类型变量的实例存在,以便 Mono 符合 EventSlotBook 它抛出此错误。 flatMap 期望将 Mono 展平到 EventSlotBook 并保存此数据
ReactiveMongoRepository
没有接受 Mono
的 save
方法。它只能接受实体类型的一个实例,所以下面的方法可行:
.flatMap(eventUserApptAfterSave -> getTestEventSlotBook(eventUserApptAfterSave)) // map changed to flatMap
.flatMap(eventSlotBookrepository::save)
有以下方法
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;
}
@存储库
public interface EventSlotBookRepository extends ReactiveMongoRepository
不确定为什么 .flatMap(eventSlotBookrepository::save) --> 编译时错误:o 类型变量的实例存在,以便 Mono 符合 EventSlotBook 它抛出此错误。 flatMap 期望将 Mono
ReactiveMongoRepository
没有接受 Mono
的 save
方法。它只能接受实体类型的一个实例,所以下面的方法可行:
.flatMap(eventUserApptAfterSave -> getTestEventSlotBook(eventUserApptAfterSave)) // map changed to flatMap
.flatMap(eventSlotBookrepository::save)