GWT验证(JSR 303)SampleValidatorFactory

时间:2013-04-04 09:34:18

标签: gwt bean-validation

我尝试在GWT中使用bean验证。我按照本指南操作: https://developers.google.com/web-toolkit/doc/latest/DevGuideValidation

在编译时,我有以下错误:

ERROR: Could not load deferred binding result type 'com.google.gwt.sample.validation.client.SampleValidatorFactory'

如果我包含这种依赖,我可以使它工作:

<dependency>
  <groupId>com.googlecode.gwt-validation</groupId>
  <artifactId>gwt-validation</artifactId>
  <version>2.1</version>
</dependency>

但我发现它很奇怪,因为指南中没有提到这种依赖性。

问题:由于GWT 2.5应该支持bean验证,为什么我需要这个额外的库?我做错了什么?

我正在使用GWT 2.5.0

我的pom.xml包含以下依赖项:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.3.1.Final</version>
  <type>jar</type>
  <classifier>sources</classifier>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.3.1.Final</version>
  <type>jar</type>
</dependency>

<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.0.0.GA</version>
  <type>jar</type>
  <classifier>sources</classifier>
</dependency>

<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.0.0.GA</version>
  <type>jar</type>
</dependency>

和我的gwt.xml包含以下几行:

<inherits name="org.hibernate.validator.HibernateValidator" />
<replace-with
  class="com.google.gwt.sample.validation.client.SampleValidatorFactory">
  <when-type-is class="javax.validation.ValidatorFactory" />
</replace-with>

在代码中,我以这种方式获得Validator:

import javax.validation.Validator;
...
private static final Validator VALIDATOR= Validation.buildDefaultValidatorFactory().getValidator();

1 个答案:

答案 0 :(得分:2)

您正在将第三方库gwt-validation与Native GWT验证混合使用。 gwt-validation不是来自官方gwt团队或google。如果您打算坚持使用本机gwt验证支持,则不应在pom中使用以下内容。

<dependency>
  <groupId>com.googlecode.gwt-validation</groupId>
  <artifactId>gwt-validation</artifactId>
  <version>2.1</version>
</dependency>

另请注意,您只有本机gwt hibernate验证器支持高达4.1.0。 GWT Sample用法。 GWT本机休眠验证器source code

您可以将其包含在

<!-- Hibernate bean validation binary for the server -->

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.1.0.Final</version>
  <exclusions>
    <exclusion>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<!-- Hibernate bean validation source for the GWT client -->

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.1.0.Final</version>
  <classifier>sources</classifier>
  <exclusions>
    <exclusion>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<!-- Required by Hibernate validator because slf4j-log4j is
     optional in the hibernate-validator POM
 -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.6.1</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.6.1</version>
</dependency>