Spring Boot / RabbitMQ 与 Jackson2JsonMessageConverter 不同 content_type

Spring Boot / RabbitMQ different content_type with Jackson2JsonMessageConverter

我有一个用例,其中服务使用不同的 RabbitMQ queue。

在配置中定义了一个 MessageConverter:

@Bean
public MessageConverter jsonMessageConverter(){
    return new Jackson2JsonMessageConverter();
}

消费方法是这样声明的:

@RabbitListener(queues = "rpc-device-cmd")
public byte[] rpcCommand(byte[] request) throws IOException, ConfigurationException { .... }

我的问题是 Jackson2JsonMessageConverter 抱怨 application/octet-stream header ( Could not convert incoming message with content-type application/octet-stream 'json' keyword missing.) 更糟糕的是,它将上述方法的 byte[] 响应编码为 JSON 和 base64。

问题:
如何确保 JSON 转换器不触及我的 byte[] 响应并忽略带有 "non-json" content_type 的消息?

您可以通过创建自己的 MessageConverter 实现来处理 rabbitMQ 响应字符串来实现此目的,如下例所示

https://github.com/nidhishkrishnan/messaging - 这里我演示了如何使用 MessageConverter

基于绑定键将 RabbitMQ 消息字符串编组和转换为 Java 域对象

在您的情况下,您可以从消息中检索内容类型以根据您的要求编组输出

String contentType = message.getMessageProperties().getContentType();