chinabugotech/hutool · error · ConvertException

Unsupported to map from [{}] of type: {}

Error message

Unsupported to map from [{}] of type: {}

What it means

PairConverter.convertInternal accepts Map.Entry, Map, CharSequence containing a separator (= / : / ,), or a readable Bean (beanToMap). Any other source leaves map null and throws ConvertException naming value and class. Mirrors EntryConverter but targets hutool's Pair.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/convert/impl/PairConverter.java:67

	protected Pair<?, ?> convertInternal(Object value) {
		Map map = null;
		if (value instanceof Map.Entry) {
			final Map.Entry entry = (Map.Entry) value;
			map = MapUtil.of(entry.getKey(), entry.getValue());
		}else if (value instanceof Map) {
			map = (Map) value;
		} else if (value instanceof CharSequence) {
			final CharSequence str = (CharSequence) value;
			map = strToMap(str);
		} else if (BeanUtil.isReadableBean(value.getClass())) {
			map = BeanUtil.beanToMap(value);
		}

		if (null != map) {
			return mapToPair(pairType, keyType, valueType, map);
		}

		throw new ConvertException("Unsupported to map from [{}] of type: {}", value, value.getClass().getName());
	}

	/**
	 * 字符串转单个键值对的Map,支持分隔符{@code :}、{@code =}、{@code ,}
	 *
	 * @param str 字符串
	 * @return map or null
	 */
	private static Map<CharSequence, CharSequence> strToMap(final CharSequence str) {
		// key:value  key=value  key,value
		final int index = StrUtil.indexOf(str, '=', 0, str.length());

		if (index > -1) {
			return MapUtil.of(str.subSequence(0, index + 1), str.subSequence(index, str.length()));
		}
		return null;
	}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Wrap the value in a Pair, Map, or Map.Entry before converting.
  2. If the source is a String, format it as "key=value" so strToMap parses it.
  3. Provide a structured source (bean with key/value fields) the converter can read.

Example fix

// before
Pair p = Convert.convert(42, Pair.class);

// after - structured source
Pair p = Convert.convert(Pair.of("value", 42), Pair.class);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(value instanceof Map) && !(value instanceof Map.Entry) && !(value instanceof Pair)
        && !(value instanceof CharSequence) && !BeanUtil.isReadableBean(value.getClass())) {
    value = Pair.of("value", value);
}
Convert.convert(value, pairType);

Type guard

value instanceof Map || value instanceof Map.Entry || value instanceof Pair
        || (value instanceof CharSequence && ((CharSequence)value).toString().matches(".*[=:,].*"))
        || BeanUtil.isReadableBean(value.getClass())

Try / catch

try {
    return Convert.convert(value, pairType);
} catch (ConvertException e) {
    if (e.getMessage().startsWith("Unsupported to map from")) { /* wrap in a Pair */ }
    else throw e;
}

Prevention

When it happens

Trigger: Calling Convert.convert(scalar, Pair.class) where scalar is a Number, primitive, enum, or other non-structured object.

Common situations: Generic conversion pipeline hitting a Pair target with a scalar source; missing key/value structure.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/a0667e68a9ef8faf. Report an issue: GitHub.