开发者

Hibernate Validator 4+. Get all Constraints associated with a class

开发者 https://www.devze.com 2023-01-06 19:53 出处:网络
How can I get all constraints for a class. For instance I have class A { @NotNull private SomeBean field;

How can I get all constraints for a class. For instance I have

class A {
   @NotNull
   private SomeBean field;
}

When I call:

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator(); 
Set<ConstraintDescriptor<?>> descriptor =  validator.getConstraintsForClass(formClass).getConstraintDescriptors();

The set is empty. I would assume there is already some functionality in Hibernate Validator that would give me all the information about constraints in a neat way , without me having开发者_C百科 to resort to the reflection api.


getConstraintsForClass() returns constraints on the class itself. To get constraints on properties, you should call getConstrainedProperties().

0

精彩评论

暂无评论...
验证码 换一张
取 消