我使用RouterFunction
在Spring应用程序中定义端点。我的服务返回一个Mono<Object>
,当端点被调用时,我想返回这个结果。我还需要进行身份验证,以便传递一个UserPrinciple
对象。
路由器
@Bean
RouterFunction<ServerResponse> router() {
return route()
.GET("/api/endpoint-name", this::getExample)
.build();
}
private Mono<ServerResponse> getExample(ServerRequest request) {
return ServerResponse.ok().body(fromPublisher(getUserPrincipal().map(service::getSomething), Object.class)).log();
}
private Mono<UserPrincipal> getUserPrincipal() {
return ReactiveSecurityContextHolder.getContext()
.map(ctx -> ctx.getAuthentication())
.map(auth -> auth.getPrincipal())
.map(UserPrincipal.class::cast);
}
服务
public Mono<Object> getSomething(UserPrincipal userPrincipal) {
WebClient webClient = getWebClient(userPrincipal.getJwt());
return webClient.get()
.uri(uriBuilder -> uriBuilder.path("another/server/endpoint").build())
.retrieve()
.bodyToMono(Object.class);
}
端点将返回以下内容:
{
"scanAvailable": true
}
这意味着我将Mono
传递到响应的主体中,而不是传递结果。但是,我使用了fromPublisher
,我认为它可以解决这个问题。
我找不到服务返回Mono
和路由正确返回Mono
结果的任何示例。
如何正确地传递Mono/Flux
作为响应的主体?
发布于 2022-04-29 15:44:06
我不会解释map
和flatMap
之间的区别,因为我已经在这里写了一个相当全面的解释:
Do you have a test to show differences between the reactor map() and flatMap()?
以上代码中的问题是Object
的返回。并将Object
的参数输入到特定的函数中。第一个函数非常直接。
Mono<UserPrincipal> = getUserPrincipal();
而第二个则会变得更多毛:
Mono<Mono<Object> value = getUserPrincipal().map(service::getSomething);
那么,为什么我们要得到一个嵌套的Mono呢?好吧,get返回一个Mono<Object>
,而根据api is Mono<R>
返回Map
,其中R
是我们从getSomething
返回的。
然后我们将其放入fromPublisher
中,这将解除第一个Mono
,最后试图序列化Mono<Object>
,从而产生奇怪的响应。
{
"scanAvailable": true
}
这里的答案是更加关注类型系统。body
函数采用Publisher
(Mono或Flux),因此不需要fromPublisher
函数。
还将map
更改为flatMap
,因为从flatMap
内部返回的类型是publisher
。
ServerResponse.ok()
.body(getUserPrincipal()
.flatMap(service::getSomething), Object.class));
https://stackoverflow.com/questions/72058522
复制相似问题