spring-projects/spring-framework · error · IllegalArgumentException
Cannot convert value of type '{}' to required type '{}'[ for
Error message
Cannot convert value of type '{}' to required type '{}'[ for property '{}']: PropertyEditor [{}] returned inappropriate value of type '{}' What it means
IllegalArgumentException from TypeConverterDelegate when a registered PropertyEditor was found and invoked but its getValue() returned an object whose type is not assignable to the required type. This indicates the PropertyEditor itself is buggy or registered against the wrong source/required type. The message names the offending editor class and the actual type it returned.
Source
Thrown at spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java:266
// but editor couldn't produce the required type...
TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue);
if (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) {
return (T) conversionService.convert(newValue, sourceTypeDesc, typeDescriptor);
}
}
// Definitely doesn't match: throw IllegalArgumentException/IllegalStateException
StringBuilder msg = new StringBuilder();
msg.append("Cannot convert value of type '").append(ClassUtils.getDescriptiveType(newValue));
msg.append("' to required type '").append(ClassUtils.getQualifiedName(requiredType)).append('\'');
if (propertyName != null) {
msg.append(" for property '").append(propertyName).append('\'');
}
if (editor != null) {
msg.append(": PropertyEditor [").append(editor.getClass().getName()).append(
"] returned inappropriate value of type '").append(
ClassUtils.getDescriptiveType(convertedValue)).append('\'');
throw new IllegalArgumentException(msg.toString());
}
else {
msg.append(": no matching editors or conversion strategy found");
throw new IllegalStateException(msg.toString());
}
}
}
if (conversionAttemptEx != null) {
if (editor == null && !standardConversion && requiredType != null && Object.class != requiredType) {
throw conversionAttemptEx;
}
logger.debug("Original ConversionService attempt failed - ignored since " +
"PropertyEditor based conversion eventually succeeded", conversionAttemptEx);
}
return (T) convertedValue;
}View on GitHub (pinned to 69bf83ad71)
Solutions
- Fix the PropertyEditor so getValue() returns an instance assignable to the required type.
- Re-check that the editor is registered for the correct requiredType / propertyPath.
- Replace the PropertyEditor with a Spring Converter / ConversionService converter, which is type-safe by signature.
Example fix
// before
public class MyEditor extends PropertyEditorSupport {
public void setAsText(String t) { setValue(Integer.valueOf(t)); } // returns Integer, required Long
}
// after
public class MyEditor extends PropertyEditorSupport {
public void setAsText(String t) { setValue(Long.valueOf(t)); }
} Defensive patterns
Strategy: validation
Validate before calling
// Sanity-check a PropertyEditor returns the required type
editor.setAsText(sample);
Object v = editor.getValue();
if (!requiredType.isInstance(v)) {
throw new IllegalStateException("editor returns " + v.getClass() + " not " + requiredType);
} Type guard
static boolean editorReturnsType(PropertyEditor e, Class<?> required, String sample) {
e.setAsText(sample); return required.isInstance(e.getValue());
} Try / catch
try {
wrapper.setPropertyValue("field", raw);
} catch (IllegalArgumentException ex) {
if (ex.getMessage().contains("returned inappropriate value")) { /* fix/replace editor */ }
throw ex;
} Prevention
- Unit-test custom PropertyEditors against representative inputs.
- Prefer typed Converter implementations over PropertyEditor where possible.
- Register editors against the exact required type, not a supertype.
When it happens
Trigger: A custom PropertyEditor registered for requiredType X whose setAsText/setValue produces a Y; or a built-in editor applied to the wrong type. Reached at line 266 when, after all conversion attempts, ClassUtils.isAssignableValue(requiredType, convertedValue) is still false and an editor participated.
Common situations: Hand-written PropertyEditor that returns a primitive wrapper or a parsed subtype; editor registered via @InitBinder against the wrong field type; editor returning null for a primitive required type.
Related errors
- Cannot convert value of type '{}' to required type '{}'[ for
- Failed to convert value of type '{}' to required type '{}'
- typeMismatch
- Nested property in path '{propertyName}' does not exist
- Failed properties: {propertyAccessExceptions}
AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09).
Data as JSON: /api/errors/c7f40f88e177ad82.
Report an issue: GitHub.