How can I set default constraints on all my properties in hibernate validator
I want to automatically put @Valid
constraints on all my properties.
Example:
public class Adult extends MyBeanValidationAbstractClass{
@Min(18)
public Inte开发者_如何学Pythonger age;
@NotBlank
public String name;
public Dog dog;
public Cat cat;
}
I want the Validator to think that all properties have the @Valid
annotation.
How can this can be achieved?
So in you example, you want Dog and cat to be annotated with @Valid. I guess Integer and String should not be affected? I dont think that you can automate this, who else but you should know, which requirements your attributes have?
--- Edit: just re-read ... you dont to write the annotations, you want to controll the Validator.
You could write a custom Constraint-Annoation for your type
@ValidAttributes
public class MyClass {
...
}
and then use a custome Validator for that annotation. Then you could use reflection to iterate over all your attributes and validate them.
The honest answer is: bean validation is not following convention over configuration - you have to put @Valid all over the place. IMHO bean validation has a fundamental design flaw.
BTW: For those who don't like boilerplate code and want to go non-official ways have a look here: https://m-m-m.github.io/maven/apidocs/net/sf/mmm/util/bean/api/Bean.html
精彩评论