5

I'm study Spring-Webflux and I just wonder should I wrap my @RequestBody object with Mono Publisher as well?

For instance: @RequestBody Mono<SavePriceViewModel> saveModel

Example:

@PostMapping("/item")
public Mono<ResponseEntity<PriceViewModel>> createHeaderAndItem(@RequestBody Mono<SavePriceViewModel> saveModel) {
   return service.createHeaderAndItem(saveModel).doOnSuccess(r -> log.debug("createHeaderAndItem() returned."));
}
Alexpandiyan Chokkan
  • 1,025
  • 1
  • 10
  • 30

1 Answers1

3

Request body need not be mono and we can return a Mono<ResponseModel> not required ResponseEntity

@PostMapping("/item")
public Mono<PriceViewModel> createHeaderAndItem(@RequestBody SavePriceViewModel saveModel) {
   return service.createHeaderAndItem(saveModel).doOnSuccess(r -> log.debug("createHeaderAndItem() returned."));
}

For more info on serializing and deserializing for check this. https://docs.spring.io/spring/docs/5.1.9.RELEASE/spring-framework-reference/web-reactive.html#webflux-codecs

When decoding to a multi-value publisher (e.g. Flux), each TokenBuffer is passed to the ObjectMapper as soon as enough bytes are received for a fully formed object. The input content can be a JSON array, or line-delimited JSON if the content-type is "application/stream+json".

Raghu Molabanti
  • 317
  • 5
  • 16
  • Thanks. But when we consider another case -> Flux. @RequestBody SavePriceViewModels will be a JSON array then mapping JSON object into POJO classes takes some time. Should we use then Flux e.g. ```@RequestBody Flux saveModels``` – Slawomir Nowak Sep 23 '19 at 20:24
  • Yes @SlawomirNowak you can use that for JSON array, Updated ans with a link for more info on this topic. – Raghu Molabanti Sep 24 '19 at 05:17
  • Yeah ok, but swagger auto generate it with @RequestBody Mono<...> – Davide Aug 16 '20 at 13:13