[英]Webflux post request: Only one connection receive subscriber allowed
[英]Only one connection receive subscriber allowed
有人可以帮助我吗? Only one connection receive subscriber allowed.
错误?
我看着Oleh Dokuka的回答,但没有帮助我。
我已经简化了代码以进行演示。 在我的实际代码中,我收到一个批量Json请求,我需要查询两个不同的表,这些表从请求主体中获取两个不同的参数,使用这两个结果调用另一个服务,并将结果发送到响应中。
路由器功能
@Bean
public RouterFunction<ServerResponse> myRoute(MyRequestHandler myRequestHandler) {
return route(RequestPredicates.POST("/api/something"), myRequestHandler::myHandlerFunction);
}
处理函数
public Mono<ServerResponse> myHandlerFunction(ServerRequest serverRequest) {
Mono<Integer> just = Mono.just(22);
//For simplification I've added String body here. In actual code I have proper json body
Mono<String> stringMono = serverRequest.bodyToMono(String.class);
Mono<String> mono = stringMono.zipWith(stringMono).map(t -> t.getT2() + t.getT1());
return ok().body(mono, String.class);
}
如果我just
在以下两个位置替换stringMono
,则代码工作正常
Mono<String> mono = stringMono.zipWith(stringMono).map(t -> t.getT2() + t.getT1());
为什么与Mono<String> mono = just.zipWith(just).map(t -> t.getT2() + t.getT1());
提前致谢。
看起来stringMono.zipWith(stringMono)
会使Spring尝试两次订阅请求的主体,这很可能是您的问题,因为ServerRequest是单播的,只能有一个订阅者。
尝试这个:
Mono<String> stringMono = serverRequest.bodyToMono(String.class).publish(body -> body.zipWith(body).map(t -> t.getT2() + t.getT1()));
publish()不会导致对正文的多个订阅。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.