chinabugotech/hutool · error · ConvertException

Unsupported source type: {}

Error message

Unsupported source type: {}

What it means

BeanConverter.convertInternal only accepts Map, ValueProvider, a readable Bean, or byte[] (Java-serialized). Any other source type (and not an empty string) reaches the final throw of ConvertException.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/convert/impl/BeanConverter.java:98

		if(value instanceof Map ||
				value instanceof ValueProvider ||
				BeanUtil.isReadableBean(value.getClass())) {
			if(value instanceof Map && this.beanClass.isInterface()) {
				// 将Map动态代理为Bean
				return MapProxy.create((Map<?, ?>)value).toProxyBean(this.beanClass);
			}

			//限定被转换对象类型
			return BeanCopier.create(value, ReflectUtil.newInstanceIfPossible(this.beanClass), this.beanType, this.copyOptions).copy();
		} else if(value instanceof byte[]){
			// 尝试反序列化
			return ObjectUtil.deserialize((byte[])value);
		} else if(StrUtil.isEmptyIfStr(value)){
			// issue#3136
			return null;
		}

		throw new ConvertException("Unsupported source type: {}", value.getClass());
	}

	@Override
	public Class<T> getTargetType() {
		return this.beanClass;
	}
}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Wrap the scalar in a Map keyed by the target bean's field names before converting.
  2. Use the correct converter for the source (NumberConverter, DateConverter, etc.) instead of BeanConverter.
  3. Implement ValueProvider for dynamic field resolution and pass that.

Example fix

// before - scalar cannot seed bean
User u = Convert.convert(42, User.class);

// after - wrap in a map
Map<String,Object> src = new HashMap<>();
src.put("id", 42);
User u = Convert.convert(src, User.class);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(value instanceof Map) && !(value instanceof ValueProvider)
        && !(value instanceof byte[]) && !BeanUtil.isReadableBean(value.getClass())) {
    // wrap in a Map or pick the right converter
    value = Collections.singletonMap("value", value);
}
beanConverter.convert(value, null);

Type guard

value instanceof Map || value instanceof byte[] || BeanUtil.isReadableBean(value.getClass())

Try / catch

try {
    return beanConverter.convert(value, null);
} catch (ConvertException e) {
    if (e.getMessage().startsWith("Unsupported source type")) { /* wrap source as Map */ }
    else throw e;
}

Prevention

When it happens

Trigger: Calling BeanConverter on a Number, primitive wrapper, single String scalar, enum, or arbitrary non-bean object that is not a Map/byte[]/ValueProvider.

Common situations: Generic pipeline routing a scalar value into a Bean target; misconfigured CopyOptions; calling Convert.convert(scalar, BeanType.class) where scalar cannot seed the bean.

Related errors


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