自定义校验

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = MyConstraint.class)
public @interface MyValid {
    String message();

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

具体逻辑

    import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MyConstraint implements ConstraintValidator<MyValid,Object> {
    @Override
    public void initialize(MyValid myValid) {
        System.out.println("innit");
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {

        return false;
    }
}
Last modification:February 24th, 2019 at 04:43 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment