1

我正在玩 rsocket-java。

有没有办法通过元数据发送自定义对象。?

我在他们的文档中看到了这个代码示例。

RSocketStrategies strategies = RSocketStrategies.builder()
    .metadataExtractorRegistry(registry -> {
        registry.metadataToExtract(fooMimeType, Foo.class, "foo");
        // ...
    })
    .build();
 return strategies;

我尝试了类似的东西。

@Bean
public RSocketStrategies strategies(){

    RSocketStrategies strategies = RSocketStrategies.builder()
        .metadataExtractorRegistry(registry -> {
            registry.metadataToExtract(someMimeType, Something.class, "something");
            // ...
        })
        .build();
     return strategies;

}

但得到这个例外。

Caused by: java.lang.IllegalArgumentException: No decoder for messaging/x.something and com.demo.Something

我应该为每种对象类型创建自己的解码器吗?

请不要显示字符串示例。问题是传递自定义对象。

4

1 回答 1

0

您应该在某处注册此 bean,请参阅https://docs.spring.io/spring-integration/docs/current/reference/html/rsocket.html

@Bean
public RSocketStrategies rsocketStrategies() {
    return RSocketStrategies.builder()
        .decoder(StringDecoder.textPlainOnly())
        .encoder(CharSequenceEncoder.allMimeTypes())
        .dataBufferFactory(new DefaultDataBufferFactory(true))
        .build();
}

@Bean
public ServerRSocketConnector serverRSocketConnector() {
    ServerRSocketConnector serverRSocketConnector = new ServerRSocketConnector("localhost", 0);
    serverRSocketConnector.setRSocketStrategies(rsocketStrategies());
    serverRSocketConnector.setMetadataMimeType(new MimeType("message", "x.rsocket.routing.v0"));
    serverRSocketConnector.setServerConfigurer((server) -> server.payloadDecoder(PayloadDecoder.ZERO_COPY));
    serverRSocketConnector.setClientRSocketKeyStrategy((headers, data) -> ""
                                    + headers.get(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER));
    return serverRSocketConnector;
}

但是,仅使用您提供的最小片段是不可能的。您能否在示例中提供可运行的示例或更多上下文?

于 2021-03-06T13:15:32.760 回答