{"record":{"id":"a60571f12ac0f00f","repo":"spring-projects/spring-framework","slug":"typemismatch","errorCode":"typeMismatch","errorMessage":"Failed to convert value of type '{}' to required type '{}'","messagePattern":"Failed to convert value of type '(.+?)' to required type '(.+?)'","errorType":"exception","errorClass":"TypeMismatchException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/TypeConverterSupport.java","lineNumber":82,"sourceCode":"\tpublic <T> @Nullable T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType,\n\t\t\t@Nullable TypeDescriptor typeDescriptor) throws TypeMismatchException {\n\n\t\treturn convertIfNecessary(null, value, requiredType, typeDescriptor);\n\t}\n\n\tprivate <T> @Nullable T convertIfNecessary(@Nullable String propertyName, @Nullable Object value,\n\t\t\t@Nullable Class<T> requiredType, @Nullable TypeDescriptor typeDescriptor) throws TypeMismatchException {\n\n\t\tAssert.state(this.typeConverterDelegate != null, \"No TypeConverterDelegate\");\n\t\ttry {\n\t\t\treturn this.typeConverterDelegate.convertIfNecessary(\n\t\t\t\t\tpropertyName, null, value, requiredType, typeDescriptor);\n\t\t}\n\t\tcatch (ConverterNotFoundException | IllegalStateException ex) {\n\t\t\tthrow new ConversionNotSupportedException(value, requiredType, ex);\n\t\t}\n\t\tcatch (ConversionException | IllegalArgumentException ex) {\n\t\t\tthrow new TypeMismatchException(value, requiredType, ex);\n\t\t}\n\t}\n\n}\n","sourceCodeStart":64,"sourceCodeEnd":87,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/TypeConverterSupport.java#L64-L87","documentation":"TypeMismatchException raised by TypeConverterSupport when the delegate throws ConversionException or IllegalArgumentException (e.g. error 185). Unlike error 187, a converter/editor was found and ran but produced an invalid value or threw during conversion. Code 'typeMismatch' is the conventional error code used by Spring's binding/validation pipeline.","triggerScenarios":"Binding \"abc\" to an int field (NumberFormatException wrapped as IllegalArgumentException -> TypeMismatchException at line 82); a Converter throwing a ConversionException; a PropertyEditor returning a value that fails a later check.","commonSituations":"Form binding / @RequestParam with a non-numeric value for a numeric field; @Value(\"${x}\") where the property string cannot be parsed to the injection type; SpEL conversion failures.","solutions":["Fix the source value so it parses cleanly into the target type.","Register or fix the Converter/PropertyEditor so it does not throw on the input.","Make the injection point optional (e.g. wrap in Optional<>) or handle the binding error in a @ExceptionHandler / @InitBinder.","Loosen the target type if the input range is wider than expected."],"exampleFix":"// before\n@Value(\"${threads}\") int threads; // property value \"unlimited\" -> TypeMismatchException\n\n// after\n@Value(\"${threads:4}\") int threads; // default to 4 when unparsable/missing","handlingStrategy":"try-catch","validationCode":"// Validate the source string parses before binding to a strict type\nif (requiredType == int.class && !StringUtils.isNumeric(raw)) {\n    throw new IllegalArgumentException(\"value '\" + raw + \"' is not numeric\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    wrapper.setPropertyValue(\"count\", raw);\n} catch (TypeMismatchException ex) {\n    // conversion ran but produced/failed value; log and surface field-level error\n    errors.rejectValue(\"count\", \"typeMismatch\");\n}","preventionTips":["Provide defaults for @Value/properties so unparseable inputs fall back cleanly.","Validate raw inputs at the controller boundary before binding.","Register @ExceptionHandler(MethodArgumentTypeMismatchException) in web layers."],"tags":["type-conversion","binding","type-mismatch"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}