如何将 javax.validation 导入到我的 Java SE 项目中?
我正在尝试添加约束检查,如此处所述 如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是我正在使用的依赖项(使用 Maven):
您可以从此存储库中获取:
Here are the dependencies I'm using (with Maven):
That you can get from this repository:
截至 2019 年的依赖项:
这会过渡性地引入对 Bean Validation API 的依赖项,因此您无需再执行此操作:
对于其他功能、表达式语言和 CDI 支持,您可能需要添加:
来源: Hibernate 验证器文档< /em>
这些都在 Maven Central Repo 中,因此您不需要添加 JBoss repo。
顺便说一句,这是我的示例便捷方法:
Origin.Wise
类似于 JAXB 的@XmlLocation Locator
。2013 年(原帖)版本为:
The dependencies as of 2019:
This transitively pulls in the dependency to the Bean Validation API, so you don't need to do this anymore:
For additional features, Expression Language and CDI support, you might need to add:
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:
The
Origin.Wise
is something like JAXB's@XmlLocation Locator
.In 2013 (the original post) the versions were:
Hibernate
之外的另一种解决方案概述
javax.validation (validation-api)
是遵循JSR 380 Java Bean 验证规范< /代码>。验证规则需要一个
validator
才能根据验证规则执行验证。验证器有很多种,例如
hibernate
(最流行的)、<Bval
Bval
是一种替代解决方案,我认为除了
Hibernate
之外它也很酷。在这里您可以遵循我的替代解决方案:MVN
实现
创建静态验证器
就这样!!!。
再说一遍,验证器,它只是一个验证器,您可以在其中轻松切换到其他验证器。
优点与缺点
它不是那么受欢迎,但你会喜欢它。
An alternative solution other than
Hibernate
Overview
javax.validation (validation-api)
is validation rules that followsJSR 380 Java Bean Validation Specification
. The validation rules need avalidator
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 besidesHibernate
. And here you can follow my alternative solution:MVN
Implementation
create static a validator
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.
如果您使用的是 spring boot 可以添加以下依赖
If you are using spring boot you can add the following dependency
仅对于 Maven 项目,才需要使用此依赖项进行验证注释:
For Maven projects only is necessary use this dependency for validation annotations: