i wrote two methods,first one calls the publisher (productDtoMono) only once and the other one calls it twice, the first one returns bad request (empty body) when called from controller while the second works perfectly and returns http ok i did my homework and i see that i can't consume the publisher more than one time why is that ?
public Mono<ProductDto> firstupdateProduct(Mono<ProductDto> productDtoMono) {
return productDtoMono.flatMap(productDto ->
productRepository.findById(productDto.getId())
.flatMap(p -> productDtoMono
.map(EntityDtoUtil::toEntity)))
.flatMap(productRepository::save)
.map(EntityDtoUtil::toDto);
}
public Mono<ProductDto> secondupdateProduct(String id, Mono<ProductDto> productDtoMono) {
return productRepository.findById(id)
.flatMap(product -> productDtoMono
.map(EntityDtoUtil::toEntity))
.flatMap(productRepository::save)
.map(EntityDtoUtil::toDto);
}
my controller method :
@PutMapping
public Mono<ResponseEntity<ProductDto>> updateProduct(@RequestBody Mono<ProductDto> productDtoMono){
System.out.println("in controller "+productDtoMono);
return productService.updateProduct(productDtoMono)
.map(ResponseEntity::ok)
.defaultIfEmpty(ResponseEntity.notFound().build());
}