在下一个API调用中使用Spring WebClient响应?



我想在下一个API调用中使用WebClient响应。因此,在调用下一个api之前,从响应中提取一些字段,并在下一个api调用中使用它们。有一种方法可以阻止WebClient响应并使用它。但是有没有办法做到不阻塞呢?我的代码是这样的

response = getUserByWebClient1(); // web client call 1
extract id from response
getRolesByUserId(id); // webclient call 2

这不是特定于WebClient,而是反应类型的一般概念,这里是反应器FluxMono

可以使用flatMap操作符来实现这一点。

// given UserService with a method "Mono<User> getCurrentUser()"
// and RolesService with a method "Mono<RoleDetails> getRolesForUser(Long userId)"
Mono<RolesDetails> roles = userService.getCurrentUser()
.flatMap(user -> rolesService.getRolesForUser(user.getId());

最新更新