Slawomir Nowak
Slawomir Nowak

Reputation: 53

Is @RequestBody should be wrapped with Mono Publisher in Spring Webflux?

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."));
}

Upvotes: 5

Views: 3552

Answers (1)

Raghu Molabanti
Raghu Molabanti

Reputation: 327

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".

Upvotes: 3

Related Questions