I have the below HandlerFunction that takes a POST request of a "BookingRecord". I get a Mono from ServerRequest using bodyToMono(), and then subscribe to the Mono, as I need the BookingRecord type back, to call a REST service to get back a Mono"", using WebClient. I have declared fare as an instance variable so that I can use it in return statement.
public class BookingHandler
{
private Mono<Fare> fare;
private WebClient webClient= = WebClient.create("http://localhost:8080");
public HandlerFunction<ServerResponse> book = request -> {
request.bodyToMono(BookingRecord.class)
.subscribe(br ->
{
fare = this.webClient.get()
.uri("/fares/get/{flightNumber}/{flightDate}", br.getFlightNumber(), br
.getFlightDate())
.retrieve()
.bodyToMono(Fare.class);
});
return ServerResponse.ok()
.body(BodyInserters.fromPublisher(fare, Fare.class));
};
}
But this code doesn't work. The subscribe doesn't seem to execute!! Why is that?
I had to change it to below code for it to work!.
request.bodyToMono(BookingRecord.class)
.subscribe(br ->
{
flightNumber = br.getFlightNumber();
flightDate = br.getFlightDate();
});
fare = this.webClient.get()
.uri("/fares/get/{flightNumber}/{flightDate}", flightNumber, flightDate)
.retrieve()
.bodyToMono(Fare.class);
So why my first code is not calling subscribe? I am using SpringBoot 2.1.0.M4.