English 中文(简体)
How do I import javax.validation into my Java SE project?
原标题:

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

最佳回答

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>
问题回答

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("
  ").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>

An alternative solution other than Hibernate

Overview

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.

And there are various validators such as hibernate (the most popular one), Bval, etc.

Bval

Bval is an alternative solution that I think It pretty cool also besides Hibernate. And here you can follow my alternative solution:

MVN

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.bval</groupId>
            <artifactId>bval-jsr</artifactId>
            <version>2.0.2</version>
        </dependency>

Implementation

create static a validator

...

import javax.validation.Validation;
import javax.validation.Validator;

import org.apache.bval.jsr.ApacheValidationProvider;
...

private static final Validator validator;

static {
        validator = Validation.byProvider(ApacheValidationProvider.class).configure().buildValidatorFactory()
                .getValidator();
    }

There you go!!!.
Again, validator, it is just a validator, in which you switch to other validators easily.

Pro&Con

It is not that popular, but You gonna like it.

For Maven projects only is necessary use this dependency for validation annotations:

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

If you are using spring boot you can add the following dependency

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-validation</artifactId> 
</dependency>




相关问题
Recommended way to develop using Jetty and Eclipse

I am currently developing a J2EE application and I would like to use Jetty. I would like to have iot integrated with Eclipse, so I could debug the appliaction. I ve tried out couple of plugins (...

Call function periodically in Java

we need run one function periodically in Java web application . How to call function of some class periodically ? Is there any way that call function when some event occured like high load in server ...

Why make an EJB rather than a Web Service?

I would have thought that there is a lot of information out there on this, but I haven t found anything that really answers my question. What are the advantages of making an EJB rather than a web ...

Where should I put this configuration setting?

I m designing a fairly small web application which will run on a Sun application server (v9.1). It only has a few pages, no database of its own, and will retrieve/update data via web services. There s ...

JNDI Names -- Is Prefix "jdbc/" needed?

What s up with JNDI names? I m trying to get a javax.sql.DataSource using the new annotations feature of Java 5. It s not working for me, so I want to ask... I have a in my web.xml, inside of it is ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

热门标签