我正在尝试使用 Spring MVC 设置表单的 JSR-303 验证。我已经正确配置了所有内容(或者至少我认为我这样做了),并且验证工作正常。但是,如果我有一个包含要验证的对象集合的命令对象,并且我使用 @Valid 注释该集合,则 Hibernate JSR-303 提供程序没有提供正确的 propertyPath。ContraintViolation 对象内的 propertyPath 应该填充为list[0].bar
and list[1].bar
,但 Hibernate 验证器只是提供list[].bar
and list[].bar
。当 Spring 的 SpringValidatorAdaptor.validate() 方法尝试添加字段级错误时,这会导致 NumberFormatException(因为它在内部期望在这些括号中存在一个数字)。
使用 spring-context-3.0.5 和 hibernate-validator-4.1.0.Final (我也尝试了 4.0.2.GA 和 4.2.0.Beta2 并得到了相同的结果),我写了一个小单元测试来说明这个问题:
package com.foo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validation;
import javax.validation.Validator;
import org.hibernate.validator.constraints.NotEmpty;
import org.junit.Test;
import org.springframework.util.AutoPopulatingList;
public class ValidatorTest {
class Person {
@NotEmpty
private String name;
@NotEmpty
@Valid
private Collection<Foo> list = new AutoPopulatingList<Foo>(Foo.class);
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Collection<Foo> getList() {
return list;
}
public void setList(Collection<Foo> foos) {
this.list = foos;
}
}
class Foo {
@NotEmpty
private String bar;
public void setBar(String bar) {
this.bar = bar;
}
public String getBar() {
return bar;
}
}
@Test
public void testValidator() throws Exception {
Foo foo0 = new Foo();
foo0.setBar("");
Foo foo1 = new Foo();
foo1.setBar("");
Collection<Foo> list = new ArrayList<ValidatorTest.Foo>();
list.add(foo0);
list.add(foo1);
Person person = new Person();
person.setName("Test Person");
person.setList(list);
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Person>> violations = validator.validate(person);
for (ConstraintViolation<Person> constraintViolation : violations) {
System.out.println(constraintViolation);
}
}
}
上述测试产生以下输出:
ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=list[].bar, rootBeanClass=class com.foo.ValidatorTest$Person, messageTemplate='{org.hibernate.validator.constraints.NotEmpty.message}'}
ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=list[].bar, rootBeanClass=class com.foo.ValidatorTest$Person, messageTemplate='{org.hibernate.validator.constraints.NotEmpty.message}'}
它产生的错误是正确的;但是, propertyPath 不是(至少据我了解)。
现在,如果我将 Hibernate 的 JSR-303 实现替换为 Apache 的(org.apache.bval.bundle-0.2-incubating--使用此处提到的依赖项),我会得到我期望的输出。这是完全相同的测试,但使用的是 Apache 的 JSR-303 注释而不是 Hibernate 的。请注意现在存在于 propertyPath 字段中的索引:
ConstraintViolationImpl{rootBean=com.foo.ValidatorTest$Person@5f989f84, propertyPath='list[0].bar', message='may not be empty', leafBean=com.foo.ValidatorTest$Foo@4393722c, value=}
ConstraintViolationImpl{rootBean=com.foo.ValidatorTest$Person@5f989f84, propertyPath='list[1].bar', message='may not be empty', leafBean=com.foo.ValidatorTest$Foo@528acf6e, value=}
由于各种原因,我可能坚持使用 Hibernate 的 JSR-303 实现。我正在做的事情是否导致 Hibernate 验证器不填充这些索引?我试图尽可能避免在我的 Spring 控制器中进行手动错误处理。
感谢您提供的任何帮助!