Hibernate Validator可以用作Hibernate之外的组件吗?

时间:2023-01-16 12:20:15

I am trying to add validation for message payload (which are json). I am using Jackson Json processor for data binding, which works quite well for me, using bean methods and occasional annotation or two.

我正在尝试为消息有效负载添加验证(这是json)。我正在使用Jackson Json处理器进行数据绑定,这对我很有用,使用bean方法和偶尔的注释或两个。

But beyond data binding, I would like to declaratively validate actual values: and specifically I prefer annotations over any external schema (DRY principle, keeping code & constraints fully in-sync). And so I am looking for a simple validation library to do this.

但是除了数据绑定之外,我想声明性地验证实际值:特别是我更喜欢注释而不是任何外部模式(DRY原则,保持代码和约束完全同步)。所以我正在寻找一个简单的验证库来做到这一点。

It looks like JSR-303 pretty much defines annotations I would need for declaration part. And it looks like Hibernate Validator is an implementation for actual validator that uses these annotations. But documentation covers use case where it is used with Hibernate, so I am not sure if it could be used outside Hibernate itself. Since I do not do any database access for the service in question, I would prefer not including other parts of Hibernate.

看起来JSR-303几乎定义了声明部分所需的注释。看起来Hibernate Validator是一个使用这些注释的实际验证器的实现。但文档涵盖了与Hibernate一起使用的用例,所以我不确定它是否可以在Hibernate之外使用。由于我没有对相关服务进行任何数据库访问,我宁愿不包括Hibernate的其他部分。

Has anyone done this or have good pointers to samples?

有没有人这样做或有很好的指针样品?

Alternatively, if anyone knows of another good JSR-303 implementation that would work as well.

或者,如果有人知道另一个好的JSR-303实现也可以。

1 个解决方案

#1


Hibernate Validator can be used at any layers. However automatic validation will not happen in your case. So you have to trigger the validation programmatically.

Hibernate Validator可以在任何层使用。但是,在您的情况下不会进行自动验证。因此,您必须以编程方式触发验证。

#1


Hibernate Validator can be used at any layers. However automatic validation will not happen in your case. So you have to trigger the validation programmatically.

Hibernate Validator可以在任何层使用。但是,在您的情况下不会进行自动验证。因此,您必须以编程方式触发验证。