gpt4 book ai didi

java - springboot-rsocket如何接收多个参数?

转载 作者:行者123 更新时间:2023-12-02 01:13:47 41 4
gpt4 key购买 nike

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在请求Controller时会报错

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 注解的处理程序方法只能绑定(bind)传入消息中的一些内容:

  • 实际的消息正文
  • 标题
  • 来自目标路线的一些变量
  • 或请求者向客户端发送请求

在您的示例中,无法绑定(bind)字符串路径、字符串方法参数。

不支持在RSocketRequester请求中设置多个数据有效负载(我认为只会发送最后一个)。您应该创建一个对象并将其作为有效负载发送。

设置 RSocket 时,使用 MimeTypeUtils.ALL_VALUE 作为数据 MIME 类型将不起作用。您需要使用实际的、具体的 MIME 类型,否则框架将不知道如何序列化您的数据。

一般来说,您应该避免手动创建 RSocket,而是 rely on the Spring infrastructure for that .

更新

与此同时,Spring 框架团队 improved the API to avoid calling data on the requester multiple times .

关于java - springboot-rsocket如何接收多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58968085/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com