chinabugotech/hutool · error · UnsupportedOperationException
Unsupported toMap value type: {}
Error message
Unsupported toMap value type: {} What it means
MapConverter.convertInternal accepts a Map or a readable Bean (BeanUtil.isReadableBean). Any other source type (primitive, scalar String that is not a bean, array, etc.) reaches the else branch and throws UnsupportedOperationException naming the value class.
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/convert/impl/MapConverter.java:86
final Class<?> mapClass = TypeUtil.getClass(this.mapType);
if (null == mapClass || mapClass.isAssignableFrom(AbstractMap.class)) {
// issue#I6YN2A,默认有序
map = new LinkedHashMap<>();
} else{
map = MapUtil.createMap(mapClass);
}
convertMapToMap((Map) value, map);
} else if (BeanUtil.isReadableBean(value.getClass())) {
if(value.getClass().getName().equals("cn.hutool.json.JSONArray")){
// issue#3795 增加JSONArray转Map错误检查
throw new UnsupportedOperationException(StrUtil.format("Unsupported {} to Map.", value.getClass().getName()));
}
map = BeanUtil.beanToMap(value);
// 二次转换,转换键值类型
map = convertInternal(map);
} else {
throw new UnsupportedOperationException(StrUtil.format("Unsupported toMap value type: {}", value.getClass().getName()));
}
return map;
}
/**
* Map转Map
*
* @param srcMap 源Map
* @param targetMap 目标Map
*/
private void convertMapToMap(Map<?, ?> srcMap, Map<Object, Object> targetMap) {
final ConverterRegistry convert = ConverterRegistry.getInstance();
srcMap.forEach((key, value)->{
key = TypeUtil.isUnknown(this.keyType) ? key : convert.convert(this.keyType, key);
value = TypeUtil.isUnknown(this.valueType) ? value : convert.convert(this.valueType, value);
targetMap.put(key, value);
});
}View on GitHub (pinned to 8870454b2a)
Solutions
- Wrap the scalar in a Map before converting.
- Use the correct target type for the source (e.g. String -> String, Number -> Number).
- Pre-check whether the source is a Map or a readable bean before targeting Map.
Example fix
// before
Map m = Convert.convert("hello", Map.class);
// after - wrap structured
Map<String,Object> src = Collections.singletonMap("value", "hello");
Map m = Convert.convert(src, Map.class); Defensive patterns
Strategy: type-guard
Validate before calling
if (!(value instanceof Map) && !BeanUtil.isReadableBean(value.getClass())) {
value = Collections.singletonMap("value", value); // or pick a different target
}
Convert.convert(value, Map.class); Type guard
value instanceof Map || BeanUtil.isReadableBean(value.getClass())
Try / catch
try {
return Convert.convert(value, Map.class);
} catch (UnsupportedOperationException e) {
if (e.getMessage().startsWith("Unsupported toMap value type")) { /* wrap scalar in a Map */ }
else throw e;
} Prevention
- Only target Map when the source is a Map or a readable bean.
- Wrap scalars in a Map if a Map truly is required.
When it happens
Trigger: Calling Convert.convert(scalar, Map.class) where scalar is a Number, Boolean, plain String, primitive array, etc.
Common situations: Misrouting a scalar through Map conversion; generic pipeline that assumes every value is bean-shaped.
Related errors
- Unsupported to map from [{}] of type: {}
- Unsupported {} to Map.
- Unsupported to map from [{}] of type: {}
- Default value [{}]({}) is not the instance of [{}]
- Unsupported source type: {}
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/cf91946d1b755cc8.
Report an issue: GitHub.