YunaiV/ruoyi-vue-pro · error · IllegalArgumentException
未知的序列化类型:{}
Error message
未知的序列化类型:{} What it means
Thrown by the private createSerializer switch inside IotMessageSerializerManager when an IotSerializeTypeEnum value is neither JSON nor BINARY. Because the constructor iterates every enum constant and the enum currently declares only JSON and BINARY, this default branch is effectively unreachable at runtime — it exists as a compile-time exhaustiveness guard. It fires only if someone adds a new enum constant to IotSerializeTypeEnum without adding a matching case here, which would crash the manager construction (and thus the whole gateway bean).
Source
Thrown at yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/serialize/IotMessageSerializerManager.java:46
log.info("[IotSerializerManager][序列化器 {} 创建成功]", type);
}
}
/**
* 根据类型创建序列化器
*
* @param type 序列化类型
* @return 序列化器实例
*/
@SuppressWarnings("EnhancedSwitchMigration")
private IotMessageSerializer createSerializer(IotSerializeTypeEnum type) {
switch (type) {
case JSON:
return new IotJsonSerializer();
case BINARY:
return new IotBinarySerializer();
default:
throw new IllegalArgumentException("未知的序列化类型:" + type);
}
}
/**
* 获取序列化器
*
* @param type 序列化类型
* @return 序列化器实例
*/
public IotMessageSerializer get(IotSerializeTypeEnum type) {
return serializerMap.get(type);
}
}
View on GitHub (pinned to 0418084e22)
Solutions
- Add a case for the new enum constant in createSerializer returning the new IotMessageSerializer implementation.
- If you did not intend to add a format, remove the stray enum constant from IotSerializeTypeEnum.
- Replace the switch with an EnumMap-based or Supplier-based registry so adding an enum constant forces registration at compile time.
- Add a unit test asserting createSerializer returns non-null for every IotSerializeTypeEnum.values() to catch the drift.
Example fix
// before
switch (type) {
case JSON: return new IotJsonSerializer();
case BINARY: return new IotBinarySerializer();
default: throw new IllegalArgumentException("未知的序列化类型:" + type);
}
// after (after adding PROTOBUF to the enum)
switch (type) {
case JSON: return new IotJsonSerializer();
case BINARY: return new IotBinarySerializer();
case PROTOBUF: return new IotProtobufSerializer();
default: throw new IllegalArgumentException("未知的序列化类型:" + type);
} Defensive patterns
Strategy: validation
Validate before calling
// Verify every enum constant has a serializer before constructing the manager
for (IotSerializeTypeEnum t : IotSerializeTypeEnum.values()) {
// createSerializer must cover t; assert in a test, not in prod code
assert hasCaseFor(t) : "createSerializer missing case for " + t;
} Prevention
- Treat createSerializer's switch and IotSerializeTypeEnum as one unit — change them together.
- Add a unit test: for every IotSerializeTypeEnum.values(), createSerializer returns a non-null IotMessageSerializer.
- Prefer a registry/EnumMap keyed by enum so a missing entry is a compile-time gap.
- Code-review any addition to IotSerializeTypeEnum for a matching serializer case.
When it happens
Trigger: Adding a third constant (e.g. PROTOBUF) to IotSerializeTypeEnum but forgetting to extend the switch in createSerializer. The manager constructor loops over values(), hits the new value, falls through to default, and throws IllegalArgumentException during Spring bean init. Cannot be triggered by any device message or config value.
Common situations: Extending the serialization framework with a new format; refactoring the enum; copy-pasting a serializer without wiring it. Because IotSerializeTypeEnum.of(type) returns null for unknown strings long before this point, user-supplied config never reaches this branch.
Related errors
AI-assisted analysis of YunaiV/ruoyi-vue-pro@0418084e22 (2026-08-14).
Data as JSON: /api/errors/8933ecc0a1620363.
Report an issue: GitHub.