解决 RocketMQ 报错 MESSAGE_ILLEGAL empty body 消息体非法为空修复
排查 RocketMQ 生产端报错 MQClientException: CODE: 13 DESC: the message body is null or empty,分析序列化与判空拦截。
问题现象
向 RocketMQ 发送消息时,客户端抛出异常并中断业务流程:
org.apache.rocketmq.client.exception.MQClientException: CODE: 13 DESC: the message body is null or empty
For more information, please visit the url: http://rocketmq.apache.org/docs/faq/
触发根因
- 构建
Message对象时传入的body字节数组为null或长度为 0; - 业务对象序列化工具(如 Jackson / FastJson / Protobuf)返回了空字节流,未对序列化结果进行前置断言保护。
解决步骤
在发送端添加前置防御性空值校验
确保消息体非空:
byte[] body = serialize(orderDTO);
if (body == null || body.length == 0) {
throw new IllegalArgumentException("Message body cannot be empty for order: " + orderDTO.getId());
}
Message msg = new Message("order_topic", "tagA", body);
producer.send(msg);
检查序列化工具配置
确保 DTO 具备标准的 Getter/Setter 方法以避免序列化结果为空 {}。
验证确认
重新触发业务测试:
序列化数据完整生成,消息顺利送达 Broker,13 号异常被彻底防御消除。


