嗨,我有 Flux 并且在迭代每个元素期间它会创建新的 mono 。我还有 Flux 之外的其他单声道。并想做以下事情:当通量(具有相应的内部单声道结束)然后做第二个单声道。具有挑战性的部分是 mono 内部的通量从 webclient 请求创建。作为起点,请查看“加载”方法。基本上没有 webclient 它可以工作,但如果 webclient 里面的地图工作之后。使用弹簧引导 2
public WebClient.ResponseSpec sendGetRequest(String path, Object... pathVariables){
try {
LOGGER.info("content type {}, url {}, path {}", contentType, url, path);
WebClient.ResponseSpec responseSpec = sendRequest(HttpMethod.GET, contentType, authorizationToken, url, path, pathVariables);
return responseSpec;
}catch (Exception e){
throw new WebClientProcessingException("Exception when trying to process", e);
}
}
public Mono<PersonPayload> loadPerson(String path){
try {
LOGGER.info("path {}", path);
Mono<QuestionDetailsPayload> person = sendGetRequest(path).bodyToMono(PersonPayload.class);
return person;
}catch (Exception e){
throw new WebClientProcessingException("Exception when trying to process",e);
}
}
public Mono<PersonDomain> getPerson(String path) {
Assert.notNull(path, "path can't be null");
try{
LOGGER.info("path {}" ,path);
Mono<PersonPayload> personPayload = loadPerson(path);
return personPayload.map(this::toPersonDomain);
}catch (Exception e){
throw new PersonNotFoundException("Exception when trying to get person info" , e);
}
}
public PersponDomain toPersonDomain(PersonPayload personPayload){
return modelMapper.map(personPayload, PersonDomain.class);
}
public void load(){
List<String> outStr = Arrays.asList("out1", "out2","out3");
Flux flux = Flux.fromIterable(outStr);
Flux<Mono<PersonDomain>> results = flux.map(string ->{
System.out.println(string);
Mono<PersonDomain> personMono = getPerson("inside");
Mono<String> result = personMono.map(h ->{
System.out.println(personMono.getName());
return personMono.getName() + "_test";
});
return result;
});
Mono<String> second = Mono.just("second");
results.then(second);
results.subscribe(stringMono -> {
stringMono.subscribe();
});
second.subscribe( s->{
System.out.println(s);
});
}
摇篮依赖:
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.postgresql:postgresql'
implementation 'org.springframework.boot:spring-boot-starter-jooq'
implementation 'org.jooq:jooq-codegen'
implementation 'org.modelmapper:modelmapper:2.3.0'
implementation 'org.modelmapper:modelmapper:2.3.0'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'com.google.code.gson:gson:2.8.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.powermock:powermock-module-junit4:2.0.0'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.0'}