0
我正在使用一個通用插件來幫助驗證基於JSR303 bean驗證的bean。 因爲它需要是通用的,所以我不能依靠底層的供應商實現。在類和屬性約束之間進行確定JSR303 bean驗證
我需要從ConstraintViolation對象中確定違反約束的類型。一種方法是使用ConstraintViolation#getPropertyPath()
。如果getName()
在leaf
節點上返回null,那麼您有一個類級約束,否則屬性級約束。
一種選擇是像
Iterator<Node> violationNodes=violation.getPropertyPath().iterator();
Node leafNode=null;
while (violationNodes.hasNext()){
leafNode=violationNodes.next();
}
if(leafNode!=null){
// property constraint
}
else{
// class constraint
}
這是很好的方法來確定或可以有其他有效的還是不錯的方法來做到這一點?
不確定到現在爲止,因爲哪個版本的'Bean Validation'使用依賴對象,但我不確定是否所有供應商都使用了1.1。如果我記得沒有問題,Apache Bean驗證器仍然無法使用1.1 –