Flatmap 在展平嵌套 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;
}
@Repository 调用 公共接口 EventSlotBookRepository 扩展了 ReactiveMongoRepository
不知道为什么 .flatMap(eventSlotBookrepository::save) -- 编译时错误:o 类型变量的实例存在,因此 Mono 符合 EventSlotBook 它会抛出此错误。 flatMap 预期扁平化 Mono
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ReactiveMongoRepository
没有可以接受Mono
的save
方法。它只能接受实体类型的实例,因此以下内容可以工作:ReactiveMongoRepository
does not have asave
method which would accept aMono
. It can only accept an instance of the entity type, so the following would work: