1 回答
TA贡献1863条经验 获得超2个赞
虽然spring的做法不是很智能,但是逻辑上是正确的。以下代码可以帮助您找到无效值。
FieldError fieldError = bindingResult.getFieldError();
if (fieldError != null && fieldError.contains(TypeMismatchException.class)) {
TypeMismatchException typeMismatchException = fieldError.unwrap(TypeMismatchException.class);
ConversionFailedException conversionFailedException = findConversionFailedException(typeMismatchException);
if (conversionFailedException != null) {
Object value = conversionFailedException.getValue();
// get the invalid field value
}
}
/**
* Recursively find the ConversionFailedException
* @param target
* @return
*/
public ConversionFailedException findConversionFailedException(Throwable target) {
Throwable cause = target.getCause();
if (cause == null) {
return null;
} else if (cause instanceof ConversionFailedException) {
return (ConversionFailedException)cause;
}
return findConversionFailedException(target);
}
添加回答
举报