验证取决于其他验证器的自定义验证器

问题描述:

现在练习了定制的hibernate验证器之后,我想了解更多。众所周知,定制休眠注释可以由一个或多个ConstraintValidator<T extends Annotation, X>验证,具体取决于X的数据类型。验证取决于其他验证器的自定义验证器

我在想,如果我能创造这将取决于特定的bean类配对约束验证器(@FooTwo@FooThree)中选择几个ConstraintValidator(为@FooOne)。例如:

@FooOne 
@FooTwo 
private String fooField; 

@FooOne 
@FooThree 
private String anotherFooField; 

不,你不能。这会违反“ConstraintValidator分辨率算法”。另请参阅Bean验证规范 - http://beanvalidation.org/1.1/spec/#typevalidatorresolution

如果对于同一类型有两个以上(最具体的)约束验证器实现,则会引发异常。

+0

我明白了。然而,会不会有其他的选择? – 2015-03-04 08:40:39

+0

这取决于你的用例。你想要达到什么目标? – Hardy 2015-03-04 08:58:08

+0

基本上,如果添加了另一个特定的自定义注释,我希望自定义注释从其验证器列表中调用一个“ConstraintValidator”,其中选定的验证器将被选中。就像'@ FooOne'会为'fooField'调用'FooOneFooTwoValidator',并为'anotherField'调用'FooOneFooTwoValidator'。 – 2015-03-05 02:15:04