In this project we gonna create own validation annotation(s) and for example we will use it in Spring MVC & Maven web application.
Check this two classes :
1 : CourseCode
In this class we need to create
@Annotation class and give it some rules like : @Constraint("tell him from which class will gate rules"),
@Target("tell to app where it can use it like {ElementType.METHOD//in method, ElementType.FIELD//in fields}"), @Retention("tell to app what the policy like: RetentionPolicy.RUNTIME")
// define default course code
public String value() default "CoderACJHP";
// define default error code
public String message() default "must start with CoderACJHP";
// define default grups
public Class<?>[] groups() default {};
// define default peyloads
public Class<? extends Payload>[] payload() default {};
2: CourseCodeConstraintValidator
In this class we need to create validation logic it's very basic, just at first make the class implements ConstraintValidator interface and implemet all methods and override them as you want.
That's it enjoy!
Thank you for reading this.