private final RSocketRequester rSocketRequester;
@RequestMapping(path = "/**")
public Publisher<ServerResponse> mockController(ServerWebExchange serverWebExchange) {
String path = serverWebExchange.getRequest().getPath().toString();
String method = serverWebExchange.getRequest().getMethodValue();
return rSocketRequester.route("/mock").data(path).data(method).retrieveMono(ServerResponse.class);
}
@MessageMapping(value = "/mock")
public Mono<ServerResponse> mockService(String path, String method) {
return Mono.just(new ServerResponse<>(0, "success", path+method));
}
如果我为Rsocket设置了更多参数,当我请求控制器时,Rsocket将报告错误
java.lang.NullPointerException: null
at io.rsocket.util.ByteBufPayload.sliceData(ByteBufPayload.java:149) ~[rsocket-core-1.0.0-RC5.jar:na]
at org.springframework.messaging.rsocket.PayloadUtils.retainDataAndReleasePayload(PayloadUtils.java:54) ~[spring-messaging-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.messaging.rsocket.annotation.support.MessagingRSocket.retainDataAndReleasePayload(MessagingRSocket.java:186) ~[spring-messaging-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) ~[reactor-core-3.3.0.RELEASE.jar:3.3.0.RELEASE]
at reactor.core.publisher.FluxJust$WeakScalarSubscription.request(FluxJust.java:99) ~[reactor-core-3.3.0.RELEASE.jar:3.3.0.RELEASE]
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:162) ~[reactor-core-3.3.0.RELEASE.jar:3.3.0.RELEASE]
at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.request(FluxPeekFuseable.java:137) ~[reactor-core-3.3.0.RELEASE.jar:3.3.0.RELEASE]
如何解决呢?
这是我的rsocket-controller配置,可能是配置问题吗?
@Bean
RSocket rSocket() {
return RSocketFactory
.connect()
.dataMimeType(MimeTypeUtils.ALL_VALUE)
.frameDecoder(PayloadDecoder.ZERO_COPY)
.transport(TcpClientTransport.create(7003))
.start()
.block();
}
@Bean
RSocketRequester rSocketRequester(RSocketStrategies rSocketStrategies) {
return RSocketRequester.builder()
.rsocketFactory(factory -> factory
.dataMimeType(MimeTypeUtils.ALL_VALUE)
.frameDecoder(PayloadDecoder.ZERO_COPY))
.rsocketStrategies(rSocketStrategies)
.connect(TcpClientTransport.create(7003))
.retry().block();
}
最佳答案
与explained in the reference documentation for RSocket一样,带注释的@MessageMapping
处理程序方法只能绑定传入消息中的一些内容:
实际的邮件正文
标头
目标路线中的一些变量
或请求者向客户端发送请求
在您的示例中,String path, String method
参数无法绑定。
不支持在RSocketRequester
请求中设置多个数据有效负载(我认为只会发送最后一个)。相反,您应该创建一个对象并将其作为有效负载发送。
设置RSocket时,对数据MIME类型使用MimeTypeUtils.ALL_VALUE
无效。您需要使用实际的具体MIME类型,否则框架将不知道如何序列化数据。
通常,应避免手动创建RSocket,而应改为rely on the Spring infrastructure for that。
更新资料
同时,Spring框架团队improved the API to avoid calling data
on the requester multiple times。