Using javax.validation in GWT throws a ClassNotFoundException runtime error

I use javax.validation in a GWT application. * I added dependencies to my pom:

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

But at runtime, I get a ClassNotFoundException:

2012-03-20 09: 46: 12,253 WARN [pool-2-thread-1] osctcAnnotationAttributesReadingVisitor [AnnotationAttributesReadingVisitor.java:91] Failed to load the class while reading annotation metadata. This is a non-fatal error, but some annotation metadata may not be available. java.lang.ClassNotFoundException: javax.validation.constraints.NotNull on org.apache.catalina.loader.WebappClassLoader.loadClass (WebappClassLoader.java:1701) ~ [catalina.jar: 7.0.26] on org.apache.catalina.loader .WebappClassLoader.loadClass (WebappClassLoader.java:1546) ~ [catalina.jar: 7.0.26] when

Any ideas?

+4
source share
2 answers

In fact, the validation system requires several things.

You need a validation API. It seems like yours, but you must remember that GWT needs a source of included files.

To make this work, you need to enable both the API and the sources of the API.

 <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> 

Without this, you will get a class of not found exceptions for the validation API.

You also need to make sure that you have added the inclusion of validation in your GWT XML module.

 <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> 

In addition, you also need to enable some kind of verification mechanism. You probably need a hibernate check if you follow the GWT bean validation guide.

To test an annotated object, you must use the provided API.

 import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import javax.validation.ConstraintViolation; /* ... snip ... */ //get validator factory using default bootstrap mechanism of the Validation library ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory(); //get a validator instance Validator validator = factory.getValidator(); //create new object Person person = new Person(); person.setFirstName("Andrew"); //validate person object Set<ConstraintViolation<Person>> violations = validator.validate(person); //should be one violation from lastName being null assert violations.size() == 1; 

Good luck.

+7
source

you must add one jar with a validation implementation, such as hibernate-validation.

+1
source

Source: https://habr.com/ru/post/1402429/


All Articles