我正在尝试使用 Spring MVC 设置表单的 JSR-303 验证。我已正确配置所有内容(或者至少我认为我是这样),并且验证大部分工作正常。但是,如果我有一个包含要验证的对象集合的命令对象,并且我使用@Valid 注释该集合,则 Hibernate JSR-303 提供程序未提供正确的 propertyPath。 ContraintViolation 对象内的 propertyPath 应该像 list[0].bar
和 list[1].bar
一样填充,但 Hibernate validator 只是提供 list[ .bar
和 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-- 使用注明的依赖项 here ),我将得到我期望的输出。这是完全相同的测试,但使用 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 validator 不填充这些索引?我尽量避免在我的 Spring Controller 中进行手动错误处理。
感谢您提供的任何帮助!
最佳答案
我在 Hibernate forums 上问过这个问题同样,它在那里得到了回答。我想在这里分享答案,以防其他人遇到此问题。
只需将 Collection 更改为 List 即可解决问题。这是更新后的单元测试:
package com.foo;
import java.util.ArrayList;
import java.util.List;
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 List<Foo> list = new AutoPopulatingList<Foo>(Foo.class);
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List<Foo> getList() {
return list;
}
public void setList(List<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("");
List<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[1].bar, rootBeanClass=class com.foo.ValidatorTest$Person, messageTemplate='{org.hibernate.validator.constraints.NotEmpty.message}'}
ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=list[0].bar, rootBeanClass=class com.foo.ValidatorTest$Person, messageTemplate='{org.hibernate.validator.constraints.NotEmpty.message}'}
关于java - Hibernate JSR303 验证和错误生成的 propertyPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5724976/