普遍接受的 Bean 验证方法

发布于 2024-12-05 14:16:31 字数 560 浏览 1 评论 0原文

因此,我一直在阅读(正在孵化的)Apache Bean Validation 项目,它看起来非常酷。看起来它是基于使用名为 constraints 的注释来装饰字段,并通过实现 Validator 接口来表现出来,有点像这样:

public class Employee
{
    @NotEmpty
    private String name;

    @NotEmpty
    @Size(max=50)
    private String email;

    // etc...
}

我知道还有其他注释处理器这可以让您自己模拟此功能,或者使用其他框架,例如 Google 的基于 AOP 的 Guice IoC 框架。

这里有人尝试过所有这些框架吗?注意权衡性能、陷阱或警告类型的建议。这个 Bean Validation 项目看起来像是我真的很想深入研究的项目,但如果事实证明有更好、更普遍接受的方法来执行 Bean 验证,那么学习这将是一个昂贵的(时间方面)课程/POJO 等具有最小冗余。

在此感谢您的任何意见或建议!

So I've been reading up on the (incubating) Apache Bean Validation project and it seems like pretty cool stuff. It looks like it's predicated on decorating fields with annotations called constraints and by implementing Validator interfaces, manifesting itself, sort of, like so:

public class Employee
{
    @NotEmpty
    private String name;

    @NotEmpty
    @Size(max=50)
    private String email;

    // etc...
}

I know there are other annotation processors out there that could allow you to emulate this functionality yourself, or perhaps using other frameworks, such as the AOP-based Guice IoC framework from Google.

Has anyboody here ever experimented with all of these frameworks? Care to weigh-in with performance, pitfall or caveat-type recommendations. This Bean Validation project looks like something I'd really like to dive in to, but it would be an expensive (timme-wise) lesson to learn if it turns out that there are better, more generally-accepted ways of performing validation of beans/POJOs and the likes with minimal redundancy.

Thanks for any comments or suggestions here!

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

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

发布评论

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

评论(1

梦魇绽荼蘼 2024-12-12 14:16:32

如果我是你,我会跳进去,尽管我可能会使用 Hibernate Validator。

Apache Bean Validation 和 Hibernate Validator 均基于 JSR303,因此也是行业标准。

Hibernate Validator 是该标准的参考实现。 http://www.hibernate.org/subprojects/validator.html

无论哪种方式,如果您坚持 JSR 标准,那么您应该能够在以后需要时切换到不同的实现。

If I were you I'd jump in, although I might use Hibernate Validator instead.

Both Apache Bean Validation and Hibernate Validator are based on the JSR303 so are industry standards.

Hibernate Validator is the reference implementation of the standard. http://www.hibernate.org/subprojects/validator.html

Either way if you stick to a JSR standard then you should be able to switch to different implementations if you need to later.

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