I have a list of SubDTO class and for each SubDTO class I want to get the datetimeoffset and set decision date, however the function from decisionService.getBySubId
returns mono object, I dont know how to convert mono to datetimeoffset?
List<SubDTO> hits = [Subto1, subto2]
public class SubDTO{
private UUID Id;
private OffsetDateTime decisionDate;
}
hits.forEach(currentItem-> currentItem.setDecisionDate(
decisionService.getBySubId(currentItem.getId(), currentItem.getTenantId())
//error on below line because it returns mono<datetimeoffset> but setter expects datetimeoffset rather mono
.map(decisionDto -> decisionDto.getDecisionDate())
);
There is actually no way to simply convert. U should read something about project reactor and reactive programming. When u are operating on mono you have to use predicates cuz u will work on asynchronus streams it means that if you want to convert an Mono to simple java Pojo you have to block the reactive stream and it would look something like that:
// reactive stream
Mono<OffsetDateTime> decisionDateMono = decisionService.getBySubId(currentItem.getId(), currentItem.getTenantId());
//blocked stream - bad practice
OffsetDateTime decisionDate = decisionDateMono.block();
Here is an example how your code should looks using reactive streams (its one of many solutions)
public static void main(String[] args) {
Flux.fromIterable(hits) //creating iterable reactive streams (Mono is a single stream - works same as flux but has always 0 or 1 value)
.flatMap(currentItem -> withSettedDecisionDate(currentItem))
.doOnNext(currentItem -> System.out.println("Item: " + currentItem + " decision date: " + currentItem.getDecisionDate()))
.doOnNext(currentItem -> doSomething(currentItem))
.subscribe();
}
private Mono<SubDTO> withSettedDecisionDate(SubDTO dto) {
return decisionService.getBySubId(dto.getId(), dto.getTenantId())
.map(decisionDate -> {
dto.setDecisionDate(decisionDate);
return dto;
});
}