Easy to handle -- spring boot's custom annotation and validation of custom validator

Spring boot's custom annotation and validation

Step 1: create entity class User

public class User {

private String userId;
private String username;
private String password;
private int age;//A verification will be customized belowMinimum ageAnnotation class


Step 2: customize the annotation MinAge

Weak Description:

The definition of the annotated class will not be explained in detail here. I think everyone knows that I won't make a fool of myself. If I don't know, I can spend two minutes searching online. It's not difficult. Here's a brief explanation:

  1. Annotation class decorated with @ interface
  2. There are four comments on the annotation class
    - @ Inherited annotation can be Inherited
    - @ Rentention annotation has three life cycles. Here is the Runtime runtime, with the longest life cycle
    - @ Documented document notes, output documents in words
    - @ Target indicates where the annotation is used
    . ElementType.TYPE – > on class name
    . ElementType.FIELD – > attribute
    . ElementType.METHOD – > method
    - @ Constraint / / the most important thing here is to associate the term with a user-defined validator, which means that the annotation takes effect

In particular, the value of the validateBy attribute of the Constarint annotation should be written to the custom validator.calss
I'm validateBy=MinAgeValidator.class

  1. To create the value attribute, note that you need to add (), and you can also set the default value with the keyword default
  2. Create the message attribute to prompt the error message
  3. Other attributes can be written but not written, so a basic annotation class is done

@Constraint(validatedBy = MinAgeValidator.class)
public @interface MinAge {

public int value() default 18;

public String message() default "";

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


Step 3: Customize Association validator MinAgeValidator

Weak Description:

  1. The custom validator MinAgeValidator implements the ConstraintValidator interface
  2. The first parameter is the custom annotation (MinAge in this case), and the second parameter is the attribute type of the entity class to be verified (Integer in this case)
  3. Two methods of replication interface -- initialize() --isValid()
  4. The first initialization method can be written, can not be written, and can be determined according to requirements
  5. The second isValid method is a logical verification method. If the return result is true, the verification is passed, or the release does not verify, and the return result is false, the verification fails
  6. The two parameters of isValid method, the first is the age of the entity class (user) to be verified, and the second is
    Parameters can be ignored. Logical verification only needs to process the first parameter
    When testing, the second parameter context is set to null

public class MinAgeValidator implements ConstraintValidator<MinAge,Integer> {

private Integer min;

public void initialize(MinAge constraintAnnotation) {

//Get the value attribute value of the MinAge annotation and assign this.min
this.min = constraintAnnotation.value();


public boolean isValid(Integer value, ConstraintValidatorContext context) {

//If the age is not empty, compare it with the value attribute value of the annotation. If it is greater than the value value of the annotation, the verification passes
return value>=min;
//If the value is empty, release is not verified
return true;


Step 4: mark the annotation on the entity class

public class User {

private String userId;
private String username;
private String password;
private int age;//A verification will be customized belowMinimum ageAnnotation class


What about?

Not so (crying)

Isn't it simple?

Guess (white eye)

Hurry up!

Kidney is not good (Liuwei Dihuang pill)

Published 2 original articles, won praise 1, visited 59
Private letter follow

Tags: Attribute Spring

Posted on Mon, 09 Mar 2020 22:21:19 -0700 by jweissig