Search code examples
javaspringspring-bootspring-annotations

Custom annotation for field value


I need to write a custom annotation that can act as a validator.

I have a field called name that need to match [a-zA-z0-9_@]. If not I need to raise an error.

I read this custom annotation article. In the I saw the example:

public String key() default "";

Can someone explain exactly what this is and how it works, as I didnt understand the explanation in the attached article. is this a method? what does this default keyword do. Can you also attach a blogpost or a source where I can study this. I am relatively new with java and do not understand this concept.


Solution

  • A custom annotation alone won't help you solve your problem as it's just some additional data on your field. You need some kind of validation framework, which will parse the annotation and then apply the validation. For your case, there should be adequate examples for every framework.