Mono<User> 以反应方式发送给用户
Mono<User> to User in a reactive way
我有一个 Mono 对象 Mono<User>
,我想以反应方式将其转换为对象。
context.getUser().map(User::getUserId);
在这里,context.getUser()
returns Mono<User>
我可以从那里得到 userId
。如何获取完整对象?
context.getUser().map(user -> {
// here you have the "complete" user object in variable "user"
});
如果您需要将值存储在变量中,您可以像
那样做
User user = context.getUser().block();
但这并不可取:
you should avoid this by favoring having reactive code end-to-end, as
much as possible. You MUST avoid this at all cost in the middle of
other reactive code, as this has the potential to lock your whole
reactive pipeline.
这样做有点违背了使用反应式编程的意义。然后它将有一个阻塞瓶颈,这正是反应式编程试图避免的事情。
我有一个 Mono 对象 Mono<User>
,我想以反应方式将其转换为对象。
context.getUser().map(User::getUserId);
在这里,context.getUser()
returns Mono<User>
我可以从那里得到 userId
。如何获取完整对象?
context.getUser().map(user -> {
// here you have the "complete" user object in variable "user"
});
如果您需要将值存储在变量中,您可以像
那样做User user = context.getUser().block();
但这并不可取:
you should avoid this by favoring having reactive code end-to-end, as much as possible. You MUST avoid this at all cost in the middle of other reactive code, as this has the potential to lock your whole reactive pipeline.
这样做有点违背了使用反应式编程的意义。然后它将有一个阻塞瓶颈,这正是反应式编程试图避免的事情。