[英]determine between class and property constraint JSR303 bean validation
I am working on a generic plugin which help in validating beans based on JSR303 bean validation. 我正在研究一个通用插件,该插件有助于基于JSR303 bean验证来验证bean。 Since it need to be generic, so i can not rely on underlying vendor implementation.
由于它需要通用,所以我不能依赖底层的供应商实现。
I need to determine type of constraint violation from ConstraintViolation object. 我需要从ConstraintViolation对象确定约束违反的类型。 one way is to use
ConstraintViolation#getPropertyPath()
. 一种方法是使用
ConstraintViolation#getPropertyPath()
。 If getName()
returns null on the leaf
node you have a class level constraint, otherwise a property level constraint. 如果
getName()
在leaf
节点上返回null,则您具有类级别约束,否则具有属性级别约束。
One option is like 一种选择是
Iterator<Node> violationNodes=violation.getPropertyPath().iterator();
Node leafNode=null;
while (violationNodes.hasNext()){
leafNode=violationNodes.next();
}
if(leafNode!=null){
// property constraint
}
else{
// class constraint
}
Is this good approach to determine or there can be other efficient or good approach to do this? 是确定这种好方法还是有其他有效的好方法呢?
If you're working with Bean Validation 1.0, relying on Node#getName()
would be the way to go (note that it should be leafNode.getName() != null
in your if statement). 如果您使用的是Bean Validation 1.0,则依靠
Node#getName()
是leafNode.getName() != null
的方法(请注意,在if语句中应为leafNode.getName() != null
)。
Bean Validation 1.1 adds a method Node#getKind()
which returns an enumeration for the different element kinds, so this would be the preferable way if you work with 1.1 already. Bean Validation 1.1添加了一个
Node#getKind()
方法,该方法返回不同元素类型的枚举,因此如果您已经使用1.1,这将是更好的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.