return Mono.error 内部地图 Spring Webflux

return Mono.error inside Map Spring Webflux

对于匹配条件,return Mono.error from code ,但给出了编译错误。我注释掉了

//return Mono.error(new RuntimeException("User Phone Exists already"));

结果:

Compilation Error: Required Type Mono<EventSlotBook>, Provided Mono<Object>

代码:

public Mono<EventSlotBook> getEventSlotBookWithAppointmentId(EventSlotBook eventSlotBookEntity) {

    Query query = new Query();
    query.addCriteria(
            new Criteria().andOperator(
                    Criteria.where("eventId").is(eventSlotBookEntity.getEventId()),
                    Criteria.where("eventConfigId").is(eventSlotBookEntity.getEventConfigId()),
                    Criteria.where("eventSlotId").is(eventSlotBookEntity.getEventSlotId())));

    return this.reactiveMongoTemplate.findOne(query, EventSlotBook.class)
            .map(eventSlotBookEntityFromDb -> {
                EventSlotBook eventSlotNewEntity = eventSlotBookEntityFromDb.toBuilder().build();

                if(eventSlotNewEntity.getEventUsers() != null) {
                    for(EventUser eventUserIno:eventSlotNewEntity.getEventUsers()) {
                        if(eventUserIno.getPhoneNumber().equalsIgnoreCase(eventSlotBookEntity.getEventUser().getPhoneNumber())){
                            //return Mono.error(new RuntimeException("User Phone Exists already"));
                        }
                    }
                }


                int maxTokenVal = Integer.valueOf(eventSlotNewEntity.getMaxTokenVal()).intValue() + 1;
                EventUser eventUser = new EventUser(eventSlotNewEntity.getEventUser().getName(),eventSlotNewEntity.getEventUser().getPhoneNumber(),String.valueOf(maxTokenVal));
                eventSlotNewEntity.getEventUsers().add(eventUser);
                eventSlotNewEntity.setMaxTokenVal(String.valueOf(maxTokenVal));
                eventSlotNewEntity.setEventUser(eventUser);


                return eventSlotNewEntity;
                //return Mono.error(new RuntimeException("Ts"));
            })
            .switchIfEmpty(getEventSlotBook(eventSlotBookEntity));
}

方法的调用者:我应该处理 Mono.error 和 return 以休息 API 该用户已经存在?。请对此提供帮助

public Mono<EventSlotBookRequestDto> saveEventSlotBook(Mono<EventSlotBookRequestDto> eventSlotBookRequestDtoMono){
    log.info("Start::SaveEventSlotBook");
    Mono<EventSlotBookRequestDto> eventDtoSaved =
            eventSlotBookRequestDtoMono.map(AppUtils::dtoToEntity)
                    .flatMap(eventSlotEntity -> getEventSlotBookWithAppointmentId(eventSlotEntity))
                    .doOnNext(eventSlotEntityBeforeSave -> {
                        log.info("@@@@BeforeSave::{}",eventSlotEntityBeforeSave);
                    })
                    .flatMap(eventSlotBookrepository::save)
                    .doOnNext( eventSlotBookAfterSave -> {
                        log.info("@@@@AfterSave::{}",eventSlotBookAfterSave);
                    })
                    .map(AppUtils::entityToDto);
    log.info("End::SaveEventSlotBook");
    return eventDtoSaved;
}

map用于将同步函数应用于每个项目,因此您不能return Mono 从中。对于 return 来自 map 的错误,您可以抛出异常并发出错误信号。作为替代方案,您可以使用 handle 运算符并使用 SynchronousSink 发出下一个或错误信号。

但是在你的情况下你需要使用 flatMap 因为 saveEventSlotBook returns Mono 并且应该被转换 asynchronously.