0

I use this method to save or update an existing recipe in my DB, the incoming object might not have an id property if the recipe is new. After saving the command to the db I need to redirect the user to the page that displays the recipe, in order to do this I need to get the id property out of the Mono returned by the recipeService.saveRecipeCommand(). How can I get this value out without calling the .block() method?

@PostMapping("recipe")
public String saveOrUpdate(@ModelAttribute("recipe") RecipeCommand command) {        
    RecipeCommand savedCommand = recipeService.saveRecipeCommand(command).block();
    return "redirect:/recipe/" + command.getId() + "/show";
}

The error I'm getting is:

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-2

2 Answers2

1

I think you should try to return the modified Mono like this:

return recipeService.saveRecipeCommand(command).map(command -> "redirect:/recipe/" + command.getId() + "/show");

I'm not sure about the syntax but you should let the framework unwrap the result of the Mono.

Balázs Németh
  • 6,222
  • 9
  • 45
  • 60
1

I solved the issue. This is the solution

  @PostMapping("recipe")
      public Mono<String> saveOrUpdate(@ModelAttribute("recipe") Mono<RecipeCommand> command) {
        return command.flatMap(
                recipeCommand -> recipeService.saveRecipeCommand(recipeCommand)
                        .flatMap(recipeSaved -> Mono.just("redirect:/recipe/" + recipeSaved.getId() + "/show"))
        );
      }