'Spring WebFlux, read ServerRequest body, apply some functions and return the result

I am using reactive programming in Spring and trying to do some operation with a POST request. My handler method looks like this:

public Mono<ServerResponse> getDataPOST(ServerRequest request) {
    Mono<Request> requestBodyMono = request.bodyToMono(Request.class);
    Mono<ServerResponse> responseBodyMono =  requestBodyMono.map(requestBodyObj -> {
        Mono<ServerResponse> response = someFunction(); //Returns Mono<ServerResponse>
        return response;
    });
    return responseBodyMono;
}

public Mono<ServerResponse> someFunction() {
    //..Some processing..
    String body = "Some body after processing";
    return ServerResponse.ok().bodyValue(body);
}

This is giving me an error as the map is returning Mono<Mono<ServerResponse> while I need Mono<ServerResponse>. How can I apply my function and return Mono?



Solution 1:[1]

It worked after replacing map with flatMap. If someone can explain why map didn't work it will be a lot of help and I will accept that answer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Piyush Shrivastava