I'm using Spring-WebFlux with Spring-Security and I need to save data depending on the currently registered user. In order to do so, in my controller, I need to retrieve the data sent from both the frontend and the principal.

After searching for a solution to my problem on the internet, I didn't find any clear way to do what I wanted, so I did my own stuff but I don't know whether it's correct.

Here's what I did:

public Mono<Order> createOrder(@Valid @RequestBody Mono<OrderViewModel> orderViewModel, Mono<Principal> principal) {
    return principal.flatMap(userPrincipal ->
        orderViewModel.flatMap(order -> this.orderService.save(order, userPrincipal))

As you can see, I have two Mono objects in my controller. Then, I kind of nest them one in the other by performing two flatMap() calls in order to return my Mono response.

Well, it works fine but I'm a beginner with WebFlux and I really don't know if this solution is "dirty" or valid. If you guys have any better approach I'll be happy to discuss it. Otherwise I hope this code fragment can help those with similar problems. :)


There is no problems at all with the supplied code.

Nesting multiple Monos is no problems and flatMaping over them in a nested fashion is somewhat common.


Not the answer you're looking for? Browse other questions tagged or ask your own question.