如何将 javax.validation 导入到我的 Java SE 项目中?

发布于 2024-08-30 00:51:58 字数 187 浏览 9 评论 0原文

我正在尝试添加约束检查,如此处所述 如何在 EclipseLink/JPA 中指定 @OneToMany 的基数

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

儭儭莪哋寶赑 2024-09-06 00:51:59

以下是我正在使用的依赖项(使用 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>

您可以从此存储库中获取:

<repositories>
  <repository>
    <id>jboss</id>
    <name>JBoss repository</name>
    <url>http://repository.jboss.org/maven2</url>
  </repository>
</repositories>

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>
因为看清所以看轻 2024-09-06 00:51:59

截至 2019 年的依赖项:

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

这会过渡性地引入对 Bean Validation API 的依赖项,因此您无需再执行此操作:

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

对于其他功能、表达式语言和 CDI 支持,您可能需要添加:

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

来源: Hibernate 验证器文档< /em>

这些都在 Maven Central Repo 中,因此您不需要添加 JBoss repo。

顺便说一句,这是我的示例便捷方法:

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() );
    }
}

Origin.Wise 类似于 JAXB 的 @XmlLocation Locator


2013 年(原帖)版本为:

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

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>
递刀给你 2024-09-06 00:51:59

Hibernate 之外的另一种解决方案

概述

javax.validation (validation-api) 是遵循 JSR 380 Java Bean 验证规范< /代码>。验证规则需要一个 validator 才能根据验证规则执行验证。

验证器有很多种,例如 hibernate(最流行的)、< Bval

Bval

是一种替代解决方案,我认为除了 Hibernate 之外它也很酷。在这里您可以遵循我的替代解决方案:

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>

实现

创建静态验证器

...

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();
    }

就这样!!!。
再说一遍,验证器,它只是一个验证器,您可以在其中轻松切换到其他验证器。

优点与缺点

它不是那么受欢迎,但你会喜欢它。

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.

原谅我要高飞 2024-09-06 00:51:59

如果您使用的是 spring boot 可以添加以下依赖

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-validation</artifactId> 
</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>
成熟的代价 2024-09-06 00:51:59

仅对于 Maven 项目,才需要使用此依赖项进行验证注释:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文