很快,在回答该问题之前,方法的
void返回类型在反应式应用程序中非常罕见。看来,此方法应该异步执行实际工作,但该方法返回一个同步类型。我已将其更改为
Mono<Void>答案。
如参考文档中所述,Spring
WebFlux确实支持验证。
但此处的最佳做法有所不同,因为方法参数可以是反应性类型。如果method参数尚未解析,则无法获得验证结果。
因此,类似的事情实际上是行不通的:
// can't have the BindingResult synchronously,// as the userCommand hasn't been resolved yetpublic Mono<Void> signup(@Valid Mono<UserCommand> userCommand, BindingResult result)// while technically feasible, you'd have to resolve // the userCommand first and then look at the validation resultpublic Mono<Void> signup(@Valid Mono<UserCommand> userCommand, Mono<BindingResult> result)
反应式运算符更惯用且更易于使用:
public Mono<Void> signup(@Valid Mono<UserCommand> userCommand) { return userCommand.onErrorResume(t -> Mono.error(...)).then();}


