Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import javax.validation into my Java SE project?

Tags:

I'm trying to add constraints checking, as described here How to specify the cardinality of a @OneToMany in EclipseLink/JPA

like image 846
simpatico Avatar asked Apr 25 '10 08:04

simpatico


People also ask

What is javax Validation Validation API?

javax. validation (validation-api) is validation rules that follows JSR 380 Java Bean Validation Specification . The validation rules need a validator in order to perform validating according to the validation rules.

What is javax validation?

Simply put, Javax validation works with two main group of annotations. Annotations to ensure that validation happens like @Valid or @Validated. Annotations to denote the kind of constraints to be enforced on a value like @NotNull, @Pattern.

How do you validate a model in Java?

Our requirements are to come up with a method of validating a model state while externalizing validation logic out of the java code. In the above definition a Bean is just a POJO, and a model is a collection of related Beans. So, for example, if Account has a collection of Addresses and the Account.

What is the difference between javax validation and Hibernate validator?

The Javax bean validation API provides the following most frequently used annotations. The Hibernate validator provides the following commonly used annotations for validation. In case of product or project development we must use both the annotations for bean validation.


2 Answers

Here are the dependencies I'm using (with Maven):

<dependencies>   <!-- Bean Validation API and RI -->   <dependency>     <groupId>javax.validation</groupId>     <artifactId>validation-api</artifactId>     <version>1.0.0.GA</version>   </dependency>   <dependency>     <groupId>org.hibernate</groupId>     <artifactId>hibernate-validator</artifactId>     <version>4.0.2.GA</version>   </dependency> </dependencies> 

That you can get from this repository:

<repositories>   <repository>     <id>jboss</id>     <name>JBoss repository</name>     <url>http://repository.jboss.org/maven2</url>   </repository> </repositories> 
like image 136
Pascal Thivent Avatar answered Sep 27 '22 21:09

Pascal Thivent


The dependencies as of 2019:

<dependency>     <groupId>org.hibernate.validator</groupId>     <artifactId>hibernate-validator</artifactId>     <version>6.0.16.Final</version> </dependency> 

This transitively pulls in the dependency to the Bean Validation API, so you don't need to do this anymore:

<dependency>     <groupId>javax.validation</groupId>     <artifactId>validation-api</artifactId>     <version>1.1.0.Final</version> </dependency> 

For additional features, Expression Language and CDI support, you might need to add:

<dependency>     <groupId>org.glassfish</groupId>     <artifactId>javax.el</artifactId>     <version>3.0.1-b09</version> </dependency>  <dependency>     <groupId>org.hibernate.validator</groupId>     <artifactId>hibernate-validator-cdi</artifactId>     <version>6.0.16.Final</version> </dependency> 

Source: Hibernate Validator documentation

These are all in Maven Central Repo, so you don't need to add the JBoss repo.

And BTW here's my example convenience method:

public static <T extends Object> void  validate( T object ) throws MigrationException {     ValidatorFactory factory = Validation.buildDefaultValidatorFactory();     Validator validator = factory.getValidator();     Set<ConstraintViolation<T>> valRes = validator.validate( object );     if( ! valRes.isEmpty() )     {         StringBuilder sb = new StringBuilder("Validation failed for: ");         if( object instanceof Origin.Wise )             sb.append( ((Origin.Wise)object).getOrigin() );         else             sb.append(object);          for( ConstraintViolation<T> fail : valRes)         {             sb.append("\n  ").append( fail.getMessage() );         }         throw new IllegalArgumentException( sb.toString() );     } } 

The Origin.Wise is something like JAXB's @XmlLocation Locator.


In 2013 (the original post) the versions were:

    <!-- BeanValidation and Hibernate Validator. -->     <dependency>         <groupId>org.hibernate</groupId>         <artifactId>hibernate-validator</artifactId>         <version>5.4.0.Final</version>     </dependency>             <dependency>         <groupId>org.glassfish</groupId>         <artifactId>javax.el</artifactId>         <version>3.0.1-b08</version>     </dependency>     <dependency>         <groupId>org.hibernate</groupId>         <artifactId>hibernate-validator-cdi</artifactId>         <version>5.4.0.Final</version>     </dependency> 
like image 27
Ondra Žižka Avatar answered Sep 27 '22 21:09

Ondra Žižka