Java 如何将if-else语句包含到反应流中

Java 如何将if-else语句包含到反应流中,java,spring-webflux,project-reactor,Java,Spring Webflux,Project Reactor,我有一个SpringWebFlux反应式服务,它接收DTO并将其插入多个表中。 有时,我们可能需要根据传入的DTO跳过插入某些表 这些是要求: 创建新客户机 如果DTO中存在客户转介,则创建新的客户转介 创建客户机辅助联系人(如果DTO中存在) 创建客户端电话(如果DTO中存在) 问题:- 不确定如何在反应流中应用if条件 有没有更好的办法 在这里,除第一个操作外,所有其他操作都可以并行运行 公共Mono createClientProfile(服务器请求){ 返回secContext.retr

我有一个SpringWebFlux反应式服务,它接收DTO并将其插入多个表中。 有时,我们可能需要根据传入的DTO跳过插入某些表

这些是要求:

  • 创建新客户机
  • 如果DTO中存在客户转介,则创建新的客户转介
  • 创建客户机辅助联系人(如果DTO中存在)
  • 创建客户端电话(如果DTO中存在)
  • 问题:-

  • 不确定如何在反应流中应用if条件
  • 有没有更好的办法
  • 在这里,除第一个操作外,所有其他操作都可以并行运行
  • 公共Mono createClientProfile(服务器请求){ 返回secContext.retrieveUser().flatMap(usr->{ 返回请求.bodytomino(ClientDto.class).flatMap(客户端->{ 返回到NewClient(客户端,usr).flatMap(clientRepository::save).flatMap(clientRes->{ 返回到newclientreferral(clientRes.getClientId(),client.get折扣(),usr) .flatMap(clientReferralRepository::save).flatMap(clientReferralRes->{ 返回到newclientsycontact(clientRes.getClientId(),client.getSecondary(),usr) .flatMap(clientSyContactRepository::save).flatMap(clientSyContactRes->{ 返回clientPhoneRepository .saveAll(toNewClientPhone(clientRes.getClientId(),client.getPhones(),usr)) .collectList().flatMap(phoneRes->{ 返回服务器响应 .created(URI.create(String.format)(CLIENT\u URI\u格式, clientRes.getClientId()) .contentType(APPLICATION_JSON).build(); }); }); }); }); }); }); } 私人单音客户费(最终长客户ID、最终折扣dto){ 转诊=转诊的次数(clientId, dto.getName()、dto.getType()、dto.getAmount()、dto.getStatus(); 返回Mono.just(转介); } client.getDiscount()可以为null,
    client.getSecondary()可以为null, client.getPhones()可以为空

    我用3种不同的方法分离流程

    public void createSyContact(ServerRequest request, long clientId) {
            secContext.retrieveUser().flatMap(usr -> {
                return request.bodyToMono(ClientDto.class).flatMap(client -> {
                    if (client.getSecondary() != null) {
                        return toNewClientSyContact(clientId, client.getSecondary(), usr)
                                .flatMap(clientSyContactRepository::save).flatMap(clientRes -> {
                                    return Mono.just(clientRes.getClientId());
                                });
                    } else {
                        return Mono.empty();
                    }
                });
            });
        }
    
        public void createReferral(ServerRequest request, long clientId) {
            secContext.retrieveUser().flatMap(usr -> {
                return request.bodyToMono(ClientDto.class).flatMap(client -> {
                    if (client.getDiscount() != null) {
                        return toNewClientReferral(clientId, client.getDiscount(), usr)
                                .flatMap(clientReferralRepository::save).flatMap(clientRes -> {
                                    return Mono.just(clientRes.getClientId());
                                });
                    } else {
                        return Mono.empty();
                    }
                });
            });
        }
    
        public Mono<Long> createClientWithPhones(ServerRequest request) {
            return secContext.retrieveUser().flatMap(usr -> {
                return request.bodyToMono(ClientDto.class).flatMap(client -> {
                    return toNewClient(client, usr).flatMap(clientRepository::save).flatMap(clientRes -> {
                        return clientPhoneRepository
                                .saveAll(toNewClientPhone(clientRes.getClientId(), client.getPhones(), usr)).collectList()
                                .flatMap(phoneRes -> {
                                    return Mono.just(clientRes.getClientId());
                                });
                    });
                });
            });
        }
    
    public void createSyContact(ServerRequest请求,长clientId){
    secContext.retrieveUser().flatMap(usr->{
    返回请求.bodytomino(ClientDto.class).flatMap(客户端->{
    if(client.getSecondary()!=null){
    返回到newclientsycontact(clientId,client.getSecondary(),usr)
    .flatMap(clientSyContactRepository::save).flatMap(clientRes->{
    返回Mono.just(clientRes.getClientId());
    });
    }否则{
    返回Mono.empty();
    }
    });
    });
    }
    public void createReferral(ServerRequest请求,长clientId){
    secContext.retrieveUser().flatMap(usr->{
    返回请求.bodytomino(ClientDto.class).flatMap(客户端->{
    if(client.get折扣()!=null){
    返回到newclientreferral(clientId,client.getDiscount(),usr)
    .flatMap(clientReferralRepository::save).flatMap(clientRes->{
    返回Mono.just(clientRes.getClientId());
    });
    }否则{
    返回Mono.empty();
    }
    });
    });
    }
    公用Mono createClientWithPhones(服务器请求){
    返回secContext.retrieveUser().flatMap(usr->{
    返回请求.bodytomino(ClientDto.class).flatMap(客户端->{
    返回到NewClient(客户端,usr).flatMap(clientRepository::save).flatMap(clientRes->{
    返回clientPhoneRepository
    .saveAll(toNewClientPhone(clientRes.getClientId(),client.getPhones(),usr)).collectList()
    .flatMap(电话资源->{
    返回Mono.just(clientRes.getClientId());
    });
    });
    });
    });
    }
    
    在这里,createClientWithPhones是强制性的,因此如果在那里检查,则不会。但是其他两种方法createReferral和createSyContact都有if检查。需要先执行createClientWithPhones,它将返回clientId。此clientId应在createReferral和CreateScontact中使用

    public Mono<ServerResponse> createClientProfile(ServerRequest request) {
            final List<Long> clinetIdList = new ArrayList<>();
            createClientWithPhones(request).subscribe(result -> {
                clinetIdList.add(result.longValue());
                createSyContact(request, result.longValue());
                createReferral(request, result.longValue());
            });
            return ServerResponse
                    .created(URI.create(String.format(CLIENT_URI_FORMAT,
                            clinetIdList.get(0))))
                    .contentType(APPLICATION_JSON).build();
            
        }
    
    公共Mono createClientProfile(服务器请求){ 最终列表clinetIdList=新的ArrayList(); createClientWithPhones(请求)。订阅(结果->{ add(result.longValue()); createSyContact(request,result.longValue()); createReferral(请求,result.longValue()); }); 返回服务器响应 .created(URI.create(String.format)(CLIENT\u URI\u格式, clinetIdList.get(0))) .contentType(APPLICATION_JSON).build(); }
    这就是处理这个问题的方法吗?

    一个简单的if语句可以在
    flatMap
    中执行,然后执行

    public Mono<String> foobar() {
        return Mono.just("foo").flatMap(value -> {
            if(value != null)
                return Mono.just("Has value");
            else
                return Mono.empty();
        }
    }
    
    foobar()
        .switchIfEmpty(Mono.just("Is empty"))
        .subscribe(output -> System.out.println(output);
    
    public Mono foobar(){
    返回Mono.just(“foo”).flatMap(值->{
    if(值!=null)
    返回Mono.just(“有价值”);
    其他的
    返回Mono.empty();
    }
    }
    foobar()
    .switchIfEmpty(Mono.just)(“I
    
    public Mono<String> foobar() {
        return Mono.just("foo").flatMap(value -> {
            if(value != null)
                return Mono.just("Has value");
            else
                return Mono.empty();
        }
    }
    
    foobar()
        .switchIfEmpty(Mono.just("Is empty"))
        .subscribe(output -> System.out.println(output);
    
    Mono<Client> save(Client client) {
        client.id = 1L;
        System.out.println("Save client: " + client.id);
        return Mono.just(client);
    }
    Mono<Phone> save(Phone phone) {
        System.out.println("Save phone: " + phone.clientId);
        return Mono.just(phone);
    }
    Mono<Referral> save(Referral referral) {
        System.out.println("Save referral: " + referral.clientId);
        return Mono.just(referral);
    }
    Mono<Contact> save(Contact contact) {
        System.out.println("Save contact: " + contact.clientId);
        return Mono.just(contact);
    }
    
    class DTO {
        Client client;
        List<Phone> phones;
        Optional<Contact> contact;
        Optional<Referral> referral;
    }
    
    class Client {
        Long id;
    }
    
    class Contact {
        Long clientId;
    }
    
    class Referral {
        Long clientId;
    }
    
    class Phone {
        Long clientId;
    }
    
    Mono<Long> doWork(Mono<DTO> monoDto) {
        return monoDto.flatMap(dto->{
            return save(dto.client).flatMap(client->{
                List<Mono<?>> publishers = new ArrayList<>();
                dto.phones.forEach(phone->{
                    phone.clientId = client.id;
                    publishers.add(save(phone));    
                });
                if ( dto.contact.isPresent()) {
                    Contact c = dto.contact.get();
                    c.clientId = client.id;
                    publishers.add(save(c));
                }
                if ( dto.referral.isPresent()) {
                    Referral r = dto.referral.get();
                    r.clientId = client.id;
                    publishers.add(save(r));
                }
                if ( publishers.size() > 0 )
                    return Mono.zip(publishers, obs->client.id);
                else
                    return Mono.just(client.id);
            });
        });
    }
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        saveClient(new Client(), null, null, null).subscribe(System.out::println);
        saveClient(new Client(), new Phone(), null, null).subscribe(System.out::println);
        saveClient(new Client(), new Phone(), new Contact(), null).subscribe(System.out::println);
        saveClient(new Client(), new Phone(), new Contact(), new Referral()).subscribe(System.out::println);
    }
    
    
    private Mono<Long> saveClient(Client client, Phone phone, Contact contact,
            Referral referral) {
        // TODO Auto-generated method stub
        DTO dto = new DTO();
        dto.client = client;
        dto.phones = new ArrayList<>();
        if ( phone != null ) dto.phones.add(phone);     
        dto.contact = Optional.ofNullable(contact);
        dto.referral = Optional.ofNullable(referral);
        return doWork(Mono.just(dto));
    }