1
votes

How to access RequestBody parameters which is of Mono type. Spring Boot Webflux reactive.

I would like to return a ResponseEntity and not a Mono.

@RequestMapping(value = "/poststudentdata", method = RequestMethod.POST, headers={"content-type=application/json"})
public ResponseEntity<String> poststudentData(@Valid @RequestBody Mono<Student> student, BindingResult bindingResult) {

    // How can i access student.getName() etc.... RequestBodt parameters
    // Not able to access as have declared Student as Mono.
}
1

1 Answers

1
votes

Don't try to return a non-reactive type when your input is provided asynchronously via a reactive type (Mono), because it means you'll likely end up blocking the IO thread on which the request was processed, which assumes non-blocking behavior of Controllers. This brings up the risk of not only blocking the current request's processing, but processing of all other requests in the application.

So change the return type to Mono<ResponseEntity>, rename student to studentMono for clarity and process your student in a map (or possibly flatMap if you have asynchronous transformations to apply):

return studentMono.map(student -> ResponseEntity.ok(student.getName()));