YunaiV/ruoyi-vue-pro · warning · IllegalArgumentException

序列化器({}) 不存在

Error message

序列化器({}) 不存在

What it means

Thrown on the serialize path in IotDeviceMessageServiceImpl.serializeDeviceMessage when messageSerializerManager.get(serializeType) returns null. Because IotMessageSerializerManager's constructor registers a serializer for every IotSerializeTypeEnum constant (JSON and BINARY), get() cannot return null for a real enum value while the manager is correctly constructed. This IllegalArgumentException is therefore a defensive guard, realistically reached only if the manager instance was not initialized, the enum gained an unregistered constant, or a null/custom serializeType slipped in.

Source

Thrown at yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/service/device/message/IotDeviceMessageServiceImpl.java:62

        IotDeviceRespDTO device = deviceService.getDeviceFromCache(productKey, deviceName);
        if (device == null) {
            throw exception(DEVICE_NOT_EXISTS, productKey, deviceName);
        }
        // 1.2 获取序列化器
        IotSerializeTypeEnum serializeType = IotSerializeTypeEnum.of(device.getSerializeType());
        Assert.notNull(serializeType, "设备序列化类型不能为空");

        // 2. 序列化消息
        return serializeDeviceMessage(message, serializeType);
    }

    @Override
    public byte[] serializeDeviceMessage(IotDeviceMessage message,
                                         IotSerializeTypeEnum serializeType) {
        // 1. 获取序列化器
        IotMessageSerializer serializer = messageSerializerManager.get(serializeType);
        if (serializer == null) {
            throw new IllegalArgumentException(StrUtil.format("序列化器({}) 不存在", serializeType));
        }

        // 2. 序列化消息
        return serializer.serialize(message);
    }

    @Override
    public IotDeviceMessage deserializeDeviceMessage(byte[] bytes,
                                                     String productKey, String deviceName) {
        // 1.1 获取设备信息
        IotDeviceRespDTO device = deviceService.getDeviceFromCache(productKey, deviceName);
        if (device == null) {
            throw exception(DEVICE_NOT_EXISTS, productKey, deviceName);
        }
        // 1.2 获取序列化器
        IotSerializeTypeEnum serializeType = IotSerializeTypeEnum.of(device.getSerializeType());
        Assert.notNull(serializeType, "设备序列化类型不能为空");

View on GitHub (pinned to 0418084e22)

Solutions

  1. Confirm IotMessageSerializerManager bean is constructed (constructor logs '序列化器 {} 创建成功' per type) and that all IotSerializeTypeEnum values are registered.
  2. Keep IotSerializeTypeEnum, createSerializer's switch, and the manager registration in sync (single source of truth).
  3. Validate device.getSerializeType() resolves to a non-null IotSerializeTypeEnum via IotSerializeTypeEnum.of(...) before reaching this method.
  4. If extending formats, register the serializer in the manager so get() returns it.

Example fix

// before
IotMessageSerializer serializer = messageSerializerManager.get(serializeType);
if (serializer == null) {
    throw new IllegalArgumentException(StrUtil.format("序列化器({}) 不存在", serializeType));
}
return serializer.serialize(message);

// after — fall back to JSON when the requested type is unavailable
IotMessageSerializer serializer = messageSerializerManager.get(serializeType);
if (serializer == null) {
    log.warn("[serialize][序列化器 {} 不存在,回退 JSON]", serializeType);
    serializer = messageSerializerManager.get(IotSerializeTypeEnum.JSON);
}
return serializer.serialize(message);
Defensive patterns

Strategy: validation

Validate before calling

// Resolve and validate the serialize type before serializing
IotSerializeTypeEnum type = IotSerializeTypeEnum.of(device.getSerializeType());
if (type == null || messageSerializerManager.get(type) == null) {
    type = IotSerializeTypeEnum.JSON; // safe default
}
messageSerializerManager.get(type).serialize(message);

Type guard

private static boolean isSerializerRegistered(IotMessageSerializerManager mgr, IotSerializeTypeEnum t) {
    return t != null && mgr.get(t) != null;
}

Try / catch

try {
    return serializeDeviceMessage(message, serializeType);
} catch (IllegalArgumentException e) { // 序列化器不存在
    log.warn("[serialize][type {} unavailable, falling back to JSON]", serializeType);
    return serializeDeviceMessage(message, IotSerializeTypeEnum.JSON);
}

Prevention

When it happens

Trigger: serializeDeviceMessage called with an IotSerializeTypeEnum for which the manager holds no entry — e.g. a new enum constant added without a matching createSerializer case (which itself would have failed at construction), or a manager that was bypassed/not wired by Spring. The preceding code path already Assert.notNull(serializeType), so a null type is not the cause here.

Common situations: Customizing the serialization layer and forgetting to populate serializerMap for a new type; DI misconfiguration producing an empty/partial manager; concurrent framework extension where a new enum constant exists in a module that the manager module does not see.

Related errors


AI-assisted analysis of YunaiV/ruoyi-vue-pro@0418084e22 (2026-08-14). Data as JSON: /api/errors/456ce3b10fdc9e92. Report an issue: GitHub.