验证后,Mongo Flux / Mono添加了元素

时间:2019-01-11 10:56:21

标签: spring mongodb spring-boot spring-webflux

我是卷筒纸助焊剂和mongo反应的新手。最tuto解释了使用这两种技术的基本方法-_-。

我已经想将受益对象添加到集合中,将受益对象和用户对象添加到用户集合中,但是在我必须验证这两个对象的唯一性之前。我有一个列表(ArrayList),在其中添加了raise异常。如果此列表中的任何内容都不为空,我将抛出带有所有引发的异常的RuntimeExeption(我使用WebExceptionHandler进行管理)

我的问题是如何阻止对db的调用,直到验证所有数据是否正常? (如何等待处理完成)

    //Function used to detect if I had any error with the DTO object 
    Mono<Beneficiary> detectFunctionalError(CreateBeneficiaryRequestDTO requestDTO) {

            return beneficiaryRepository.findBeneficiariesByName(requestDTO.getBeneficiaryName()).log().doOnSuccessOrError((data, errors)->{
                if(data!=null){
                    functionalExceptionHandler.appendException(O2CCodeError.O2C003);
                    LOGGER.error(O2CCodeError.O2C003.getCode() + ":" + O2CCodeError.O2C003.getDescription() + ":" + O2CCodeError.O2C003.getSource()
                            + ":" + O2CCodeError.O2C003.getType());
                }
                userService.findUserByEmail(requestDTO.getBeneficiaryEmail()).log().doOnSuccessOrError((userData, exceptions)->{
                    if (userData!=null){
                        functionalExceptionHandler.appendException(O2CCodeError.O2C004);
                        LOGGER.error(O2CCodeError.O2C004.getCode()+":"+O2CCodeError.O2C004.getDescription()+":"+O2CCodeError.O2C004.getSource()
                                +":"+O2CCodeError.O2C004.getType());
                    }

//this call responsable to call RuntimeExeption
                    functionalExceptionHandler.checkExceptions();
                    //Mono.empty();
                });
            });

    }



    public Mono<Beneficiary> addBeneficiary(CreateBeneficiaryRequestDTO requestDTO) {

        this.detectFunctionalError(requestDTO);
      //continue the save...

}

2 个答案:

答案 0 :(得分:0)

我建议您这样:

Mono<Beneficiary> found = beneficiaryRepository.findBeneficiariesByName(requestDTO.getBeneficiaryName());

return found.flatMap(beneficiary-> {
            if(beneficiary== null)
                return Mono.empty(); // never called
            else
                return Mono.error(new RuntimeException("Already exist " + beneficiary.getId()));

        }).switchIfEmpty(Mono.just(requestDTO.getBeneficiaryObjectToSave())); // new object here which want to save.  

答案 1 :(得分:0)

1) 检查null是否不正确! Reactor API is null safety

如果找到一些结果,则 Spring Data 会发出找到的值,否则返回空Flux/Mono

像这样收集您的错误:

beneficiaryRepository.findBeneficiariesByName(requestDTO.getBeneficiaryName())
    .log()
    .doOnNext(data -> functionalExceptionHandler.appendException(O2CCodeError.O2C003))
    ...
    .doOnNext(userData -> functionalExceptionHandler.appendException(O2CCodeError.O2C004);)

2) 将您的发布者和呼叫检查运营商整合到一个链中。

 return beneficiaryRepository.findBeneficiariesByName(requestDTO.getBeneficiaryName())
    .doOnNext(... //collect errors)
    .compose(flux -> userService.findUserByEmail(requestDTO.getBeneficiaryEmail())
        .doOnNext(...//collect errors)
    )
    .then(Mono.defer(() -> functionalExceptionHandler.checkExceptions() ? Mono.empty() : Mono.error(RuntimeException::new)));

此后

public Mono<Beneficiary> addBeneficiary(CreateBeneficiaryRequestDTO requestDTO) {

    this.detectFunctionalError(requestDTO)
        .switchIfEmpty(// your save logic)

如果发生错误,将停止发射,否则切换到保存逻辑