I have a reactive rest api (webflux), also using the spring WebClient class to request data from other leisure services.
Simplified design:
@PostMapping(value = "/document") public Mono<Document> save(@RequestBody Mono<Document> document){
In other words: I understand (almost) all reactive examples individually, but I cannot put it all together and build a simple usage example (get → validate → save → return) without blocking objects.
The closer I could get, the lower:
@PostMapping(value = "/document") public Mono<Document> salvar(@RequestBody Mono<Document> documentRequest){ return documentRequest .transform(this::getDocumentOwner) .transform(this::validateDocumentOwner) .and(documentRequest, this::setDocumentOwner) .transform(this::saveDocument); }
Helper Methods:
private Mono<DocumentOwner> getDocumentOwner(Mono<Document> document) { return document.flatMap(p -> documentOwnerConsumer.getDocumentOwner(p.getDocumentOwnerId())); } private Mono<DocumentOwner> validateDocumentOwner(Mono<DocumentOwner> documentOwner) { return documentOwner.flatMap(do -> { if (do.getActive()) { return Mono.error(new BusinessException("Document Owner is Inactive")); } return Mono.just(do); }); } private DocumentOwnersetDocumentOwner(DocumentOwner documentOwner, Document document) { document.setDocumentOwner(documentOwner); return document; } private Mono<Document> saveDocument(Mono<Document> documentMono) { return documentMono.flatMap(documentRepository::save); }
I use Netty, SpringBoot, spring WebFlux and Reactive Mongo Repository. But there are some problems:
1) I get an error: java.lang.IllegalStateException: only one connection recipient is allowed. Maybe because I use the same documentRequest for conversion and setDocumentOwner. I really do not know.
2) the setDocumentOwner method is not called. Thus, the document object to be saved is not updated. I believe that there may be a better way to implement this setDocumentOwner ().
thanks
source share