Why add @ NotBlank to check what is the principle that is not empty?

Why do you add @ NotBlank to verify that it is not empty? what is the principle?

Jan.21,2022

you can take a look at the principle of java annotations. Annotations are actually a class that defines some properties. In fact, the implementation of those functions is not in the annotation class, but in the caller

.

the caller acquires comments on properties or methods through reflection to achieve some functions

A single annotation is useless. There must be a caller for each annotation
for example, I have a User class

.
class User {
    @NotBlank
    private String username;
    //...
}

I can implement a method

boolean verification(User user){
    //user
    //, , NotBlank, false
    //true
}

this is a simple example. You can see that @ NotBlank just acts as a tag

. The function of

other annotations is roughly the same, but some are not called so obviously that people think that adding annotations naturally has this function.
just like @ RequestMapping in Spring , it also implements url mapping

by proxying Controller .
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields){
    // @NotEmpty
    if(field.isAnnotationPresent(NotEmpty.class)){
        field.setAccessible(true);
        //  
        if(field.get(arg) == null){
            // value
            NotEmpty annotation = field.getAnnotation(NotEmpty.class);
            Method value = annotation.annotationType().getDeclaredMethod("value");
            value.setAccessible(true);
            throw new EmptyException(value.invoke(annotation).toString());
        }
    }
}
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotEmpty {
    //
    public String value() default "";
}

I wrote the @ NotEmpty annotation of my previous project myself, and then this code is used to verify that it is empty


this is a Bean Validation specification, and you can also define your own validator for model verification
Welcome to the Bean Validation parameter verification notes of [chivalrous Dream Development Notes]

.
Menu