【问题标题】:How does springboot-rsocket receive multiple parameters?springboot-rsocket如何接收多个参数?
【发布时间】:2020-03-16 23:34:01
【问题描述】:
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设置了更多的参数,我请求Controller时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();
}

【问题讨论】:

    标签: java spring-boot rsocket


    【解决方案1】:

    作为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 Framework 团队improved the API to avoid calling data on the requester multiple times

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 2020-12-01
      • 2021-03-19
      相关资源
      最近更新 更多