3

我想直接使用官方 rsocket .net 库在 RSocket 服务器(使用 spring boot rsocket 用 java 编写)和 C# 客户端之间进行通信:https ://github.com/rsocket/rsocket-net

不幸的是,Java 服务器无法解码有效负载并将其映射到 Java 类。Obvisouly 它确实适用于字节数组,但我如何将它映射到对象。

这是服务器配置(超过 tcp:7000)

spring:
  rsocket:
    server:
      port: 7000
      transport: tcp 

我的服务器基本上有一个控制器,我希望将数据映射到一个 java 对象(这里是 Person):

@Controller
@Log4j2
public class RSocketController {

    @MessageMapping("")
    void reqFireAndForget(Person data) {
        log.info("Received raf request...");
        log.info(data.getName());
    }

}

Person java 类是这样的:

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
public class Person implements Serializable {
    int Id;
    String Name;
    String Address;
}

客户端基本上是官方github例子使用tcp Socket传输到spring boot rsocket服务器

class Program
    {
        static async Task Main(string[] args)
        {
            var client = new RSocketClient(new SocketTransport("tcp://localhost:7000/"), new RSocketOptions() { InitialRequestSize = 3 });
            await client.ConnectAsync();
            Console.WriteLine("Requesting Raw Protobuf Stream...");

            var persondata = new Person() { Id = 1234, Name = "Someone Person", Address =  "123 Any Street" };
            var personmetadata = new Person() { Id = 567, Name = "Meta Person", Address = "" };

            client.RequestFireAndForget(data: ProtobufNetSerializer.Serialize(persondata), metadata: ProtobufNetSerializer.Serialize(personmetadata));

            Console.ReadKey();

        }
    }

C# Person 类如下:

    [ProtoContract]
    class Person
    {
        [ProtoMember(1)] public int Id { get; set; }
        [ProtoMember(2)] public string Name { get; set; }
        [ProtoMember(3)] public String Address { get; set; }
        public override string ToString() => $"{Id}:{Name} ({Address})";
    }

选项是:

public static readonly RSocketOptions Default = new RSocketOptions()
        {
            KeepAlive = TimeSpan.FromMinutes(1),
            Lifetime = TimeSpan.FromMinutes(3),
            DataMimeType = "application/x-protobuf",
            MetadataMimeType = "application/x-protobuf",
        };

也许可以使用带有杰克逊对象映射器或其他东西的 Protobuf 编码器,我还没有找到任何相关的东西

这是堆栈:


2020-09-07 15:54:50.005 DEBUG 31324 --- [ctor-http-nio-4] io.rsocket.FrameLogger                   : receiving -> 
Frame => Stream ID: 1 Type: REQUEST_FNF Flags: 0b100000000 Length: 62
Metadata:
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 08 b7 04 12 0b 4d 65 74 61 20 50 65 72 73 6f 6e |.....Meta Person|
|00000010| 1a 00                                           |..              |
+--------+-------------------------------------------------+----------------+
Data:
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 08 d2 09 12 0e 53 6f 6d 65 6f 6e 65 20 50 65 72 |.....Someone Per|
|00000010| 73 6f 6e 1a 0e 31 32 33 20 41 6e 79 20 53 74 72 |son..123 Any Str|
|00000020| 65 65 74                                        |eet             |
+--------+-------------------------------------------------+----------------+
2020-09-07 15:54:50.006 DEBUG 31324 --- [ctor-http-nio-4] o.s.m.h.i.reactive.InvocableHelper       : Invoking RSocketController#reqFireAndForget[1 args]
2020-09-07 15:54:50.006 DEBUG 31324 --- [ctor-http-nio-4] o.s.m.h.i.reactive.InvocableHelper       : Searching for methods to handle MethodArgumentResolutionException
2020-09-07 15:54:50.008 ERROR 31324 --- [ctor-http-nio-4] o.s.m.h.i.reactive.InvocableHelper       : No exception handling method

org.springframework.messaging.handler.invocation.MethodArgumentResolutionException: Could not resolve method parameter at index 0 in void ch.akatech.images.controllers.RSocketController.reqFireAndForget(ch.akatech.images.entities.Person): Cannot decode to [ch.akatech.images.entities.Person]GenericMessage [payload=DefaultDataBuffer (r: 0, w: 35, c: 35), headers={dataBufferFactory=DefaultDataBufferFactory (preferDirect=true), rsocketRequester=org.springframework.messaging.rsocket.DefaultRSocketRequester@3b73055b, lookupDestination=, contentType=application/x-protobuf, rsocketFrameType=REQUEST_FNF}]
    at org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver.decodeContent(PayloadMethodArgumentResolver.java:261) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver.resolveArgument(PayloadMethodArgumentResolver.java:168) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:120) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.messaging.handler.invocation.reactive.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:183) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.messaging.handler.invocation.reactive.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:128) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.messaging.handler.invocation.reactive.InvocableHelper.handleMessage(InvocableHelper.java:194) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:458) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.handleMatch(MessageMappingMessageHandler.java:330) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler.handleMatch(MessageMappingMessageHandler.java:83) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:453) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.messaging.rsocket.annotation.support.MessagingRSocket.lambda$handle$1(MessagingRSocket.java:151) ~[spring-messaging-5.2.8.RELEASE.jar:5.2.8.RELEASE]

我希望你能帮帮我 !

AB

4

0 回答 0