栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Spring Reactive时如何验证Mono

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Spring Reactive时如何验证Mono

很快,在回答该问题之前,方法的

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();}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/403011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号