turn NotReadablePropertyException into JSR-303 oriented IllegalStateException

This commit is contained in:
Juergen Hoeller 2010-06-07 22:30:11 +00:00
parent 4cef52a86f
commit 5f9b2db90b
1 changed files with 13 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import javax.validation.ConstraintViolation;
import javax.validation.metadata.BeanDescriptor; import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor; import javax.validation.metadata.ConstraintDescriptor;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
@ -76,10 +77,17 @@ public class SpringValidatorAdapter implements Validator, javax.validation.Valid
String field = violation.getPropertyPath().toString(); String field = violation.getPropertyPath().toString();
FieldError fieldError = errors.getFieldError(field); FieldError fieldError = errors.getFieldError(field);
if (fieldError == null || !fieldError.isBindingFailure()) { if (fieldError == null || !fieldError.isBindingFailure()) {
errors.rejectValue(field, try {
violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(), errors.rejectValue(field,
getArgumentsForConstraint(errors.getObjectName(), field, violation.getConstraintDescriptor()), violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(),
violation.getMessage()); getArgumentsForConstraint(errors.getObjectName(), field, violation.getConstraintDescriptor()),
violation.getMessage());
}
catch (NotReadablePropertyException ex) {
throw new IllegalStateException("JSR-303 validated property '" + field +
"' does not have a corresponding accessor for Spring data binding - " +
"check your DataBinder's configuration (bean property versus direct field access)", ex);
}
} }
} }
} }