开发者

How can I apply a @NotNull constraint to all fields in a class using javax.validation?

开发者 https://www.devze.com 2023-01-15 18:12 出处:网络
I\'m using the hibernate implementation of the javax.validation and would like to know how whether anyone has been able to apply the @NotNull constraint an开发者_如何学Pythonnotation to all fields of

I'm using the hibernate implementation of the javax.validation and would like to know how whether anyone has been able to apply the @NotNull constraint an开发者_如何学Pythonnotation to all fields of a class, rather than to each individual field?

I would prefer to enforce the @NotNull constraint as a default across my project, to avoid littering the code with @NotNull annotations on every field. Ideally it would be preferable to define a custom constraint "@MayBeNull" to annotated any fields that may be null.


Does it really make sense in your case to validate all fields against null? What about for example with basic types like int, long, etc

Anyways, Bean Validation does not offer the functionality you are asking for. You could write a custom class constraint @MyNotNull which via reflection validated all fields against null. Personally I recommend against it.

0

精彩评论

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