1

I've recently become more fond of javax validation annotations and creating custom ones when I feel they may be appropriate. But... I can't figure if it's design-appropriate to use them for everything.

I have a REST api created using Spring Framework (Spring Boot 2 to be more specific) where I'd like to validate every field that comes through POST endpoints in my controllers. The body is written in a separate class and contains some simple javax.validation annotations such as @NotEmpty, @Size etc.

Now, for example... An endpoint for user registration should validate whether user's desired username and email address is unique. I thought about creating custom annotations (interfaces - validators) for these so I can simply add the annotation above the field I want to validate, but I'm not sure if this is appropriate. If I want to validate field's uniqueness, I will have to autowire a repository that my application is using and check if provided email or username already exists. However, this means my @Service is only good for contacting the repository in many occassions if everything is validated through validation annotations.

So my question is - should field validations for (e.g.) uniqueness be written in a @Service or javax annotations? What's more appropriate?

Many thanks!

closed as too broad by Robert Harvey Jun 25 at 18:19

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.