I'm trying to learn reactive programming by having a non-blocking rest API. I was expecting it to reply to me immediately and do the service invocation in the background. But I could see that it is waiting for the service response and does not return the acknowledgment response object.
@PostMapping("/postflow/{incidentNumber}")
public Mono<ResponseEntity<AcknowledgeResponse>> acknowledgeFlow(@PathVariable String number,
@RequestHeader("correlationId") String correlationId) {
return handler.handlePostFlow(number, correlationId)
.thenReturn(ResponseEntity.ok().body(
AcknowledgeResponse.builder().status(HttpStatus.OK.value()).build()));
}
Handler code-
public Mono<ServiceResponse> handlePostFlow(String number, String correlationId) {
return service.getDetails(number, correlationId)
.onErrorMap(throwable -> new RuntimeException("Something went wrong", throwable))
.switchIfEmpty(Mono.error(new RuntimeException(" Empty response from the service ")))
.filter(this::isServiceResultValid)
.switchIfEmpty(Mono.error(new RuntimeException("Invalid response from the service")))
.map(serviceResponse -> serviceResponse);
}
AcknowledgeResponse class structure-
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class AcknowledgeResponse {
private Integer status;
@Builder.Default
private Object data = "Acknowledged";
}
Can someone assist me in understanding what is wrong here? TIA.