vertx IllegalArgumentException: No message codec for type - 如何为自定义类型创建消费者

vertx IllegalArgumentException: No message codec for type - how to create a consumer for a custom type

我有一个创建事件总线消费者的 Verticle,如下所示:

    public void start() {
        vertx.eventBus().consumer(ADDRESS_REQUEST, this::handleRequestMessage);

    }

    private void handleRequestMessage(Message<VWApiConversation> msg) {

       VWApiConversation conversation = msg.body();

    }

但是当向这个地址发送消息时:

 vertx.eventBus().send(VehicleStateCoordinatorVerticle.ADDRESS_REQUEST, conversation, deliveryOptions, res -> {

...

我收到错误:

java.lang.IllegalArgumentException: No message codec for type: class com.vulog.vwgateway.model.VWApiConversation

我是不是漏掉了什么?

Vert.x 默认支持序列化 JVM 基元、Buffers 和 JsonObjects。对于其他自定义类型,您需要编写自己的 MessageCodec

这里有一些可能有用的文档:

  • 官方docs对此有一些说明。您会对标题为 "Types of messages" 的部分特别感兴趣。
  • here 是一个示例 MessageCodec 实现。 (此片段中未显示通过 EventBus.registerCodec() 注册编解码器。)

根据我的喜好,我一直使用 JsonObject 作为消息传递媒介(因为我的设置使我能够这样做)。为每种域类型编写自定义(反)序列化程序似乎很麻烦。