我正在尝试在GWT 2.7中使用validator.validateValue()方法,并且它似乎对集合上的@Size注释不起作用。
我正在尝试验证这个POJO ......:
public class Person {
@Size(min = 3, message = "Name not long enough")
private String mName;
@Size(min = 3, message = "Not enough course subscriptions")
private List<String> mCourses;
public String getName() {
return mName;
}
public void setName(String pName) {
mName = pName;
}
public void setCourses(final List<String> pCourses) {
mCourses = pCourses;
}
public List<String> getCourses() {
return mCourses;
}
}
...使用此验证方法:
void validate() {
final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
final Person person = new Person();
person.setName("Ed");//too short
person.setCourses(Arrays.asList("Math", "Bio"));//not enough
System.out.println("with validate object");
final Set<ConstraintViolation<Person>> violations = validator.validate(person);
for (ConstraintViolation<Person> violation : violations) {
System.out.println(violation.getMessage());
}
System.out.println("with validateValue");
final Set<ConstraintViolation<Person>> violationsName = validator.validateValue(Person.class, "mName", person.getName());
for (ConstraintViolation<Person> violation : violationsName) {
System.out.println(violation.getMessage());
}
final Set<ConstraintViolation<Person>> violationsCourses = validator.validateValue(Person.class, "mCourses", person.getCourses());
for (ConstraintViolation<Person> violation : violationsCourses) {
System.out.println(violation.getMessage());
}
}
在普通Java中运行此代码时我正确获取:
with validate object
Name not long enough
Not enough course subscriptions
with validateValue
Name not long enough
Not enough course subscriptions
但是当在GWT中运行此代码时,我错误地获取:
with validate object
Name not long enough
Not enough course subscriptions
with validateValue
Name not long enough
似乎GWT 2.7中的Validator.validateValue()方法没有正确验证应用于Collection的@Size注释。
有什么想法吗?提前谢谢!
答案 0 :(得分:0)
我对String属性有同样的问题。 当我重新启动SDM时,它运行正常。
严重的是,每次验证更改后都必须重新启动SDM。