Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using javax.validation in GWT throws runtime error ClassNotFoundException

I'm using in GWT application the javax.validation.* I added the dependencies to my pom:

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

But on runtime i'm getting ClassNotFoundException :

2012-03-20 09:46:12,253 WARN [pool-2-thread-1] o.s.c.t.c.AnnotationAttributesReadingVisitor [AnnotationAttributesReadingVisitor.java:91] Failed to classload type while reading annotation metadata. This is a non-fatal error, but certain annotation metadata may be unavailable. java.lang.ClassNotFoundException: javax.validation.constraints.NotNull at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701) ~[catalina.jar:7.0.26] at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546) ~[catalina.jar:7.0.26] at

Any ideas?

like image 265
Ben Bracha Avatar asked Jan 17 '23 04:01

Ben Bracha


1 Answers

There are actually several things you need for the validation framework to work.

You need the validation API. It seems like you have that but you must remember that GWT needs the source of included files.

In order to get this to work you need to include both the API jar and API sources.

<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'll get class not found exceptions for the validation API.

You also need to make sure that you've added the validation inclusion to your GWT module 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>

Further, you also need to include a validation engine of some sort. You probably want hibernate-validation if you're following the GWT bean validation guide.

To validate an annotated object, you should use the API provided.

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.

like image 171
Chris Ruffalo Avatar answered Jan 20 '23 15:01

Chris Ruffalo