Access post方法json请求webflux响应式编程中的Body参数

问题描述 投票:0回答:1

如何访问Mono类型的RequestBody参数。 Spring Boot Webflux反​​应灵敏。

我想返回一个ResponseEntity而不是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.
}
spring-boot spring-webflux project-reactor reactive
1个回答
1
投票

当您通过反应类型(Mono)异步提供输入时,不要尝试返回非反应类型,因为这意味着您可能最终会阻止处理请求的IO线程,这假设是非阻塞的控制器的行为。这带来了不仅阻止当前请求的处理,而且还处理应用程序中所有其他请求的风险。

因此,为了清晰起见,将返回类型更改为Mono<ResponseEntity>,将student重命名为studentMono,并在map中处理您的学生(如果您要应用异步转换,则可能需要flatMap):

return studentMono.map(student -> ResponseEntity.ok(student.getName()));
© www.soinside.com 2019 - 2024. All rights reserved.