【问题标题】:GWT vs bean validation inconsistencyGWT 与 bean 验证不一致
【发布时间】:2015-04-25 19:39:22
【问题描述】:

我正在尝试在 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 注释。

有什么想法吗?提前谢谢!

【问题讨论】:

  • +1 用于提出明确的问题。这看起来很奇怪,因为我在 GWT 2.6 上从来没有遇到过这个问题。我没有尝试使用 GWT 2.7

标签: java validation gwt bean-validation


【解决方案1】:

我对 String 属性也有同样的问题。 当我重新启动 SDM 时,它运行良好。

糟糕的是,每次验证更改后都必须重新启动 SDM。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多