Detailed explanation of spring boot validation parameter validation custom annotation rules and grouping validation

preface

Hibernate Validator is the reference implementation of Bean Validation. Hibernate Validator provides the implementation of all built-in constraints in JSR 303 specification. In addition, there are some additional constraints
In daily development, Hibernate Validator is often used to verify the fields of bean s. Based on annotations, it is convenient, fast and efficient.

In SpringBoot, @ Validated can be used to annotate the enhanced version of Hibernate Validator, or the original Bean Validation java version of @ Valid can be used

Built in verification annotation

Built in constraint in Bean Validation

annotationeffect
@ValidThe annotated element is an object, and all field values of this object need to be checked
@NullThe annotated element must be null
@NotNullThe annotated element must not be null
@AssertTrueThe annotated element must be true
@AssertFalseThe annotated element must be false
@Min(value)The annotated element must be a number and its value must be greater than or equal to the specified minimum value
@Max(value)The annotated element must be a number and its value must be less than or equal to the specified maximum value
@DecimalMin(value)The annotated element must be a number and its value must be greater than or equal to the specified minimum value
@DecimalMax(value)The annotated element must be a number and its value must be less than or equal to the specified maximum value
@Size(max, min)The size of the annotated element must be within the specified range
@Digits (integer, fraction)The annotated element must be a number and its value must be within an acceptable range
@PastThe annotated element must be a past date
@FutureThe annotated element must be a future date
@Pattern(value)The annotated element must conform to the specified regular expression

constraint attached to Hibernate Validator

annotationeffect
@EmailThe annotated element must be an email address
@Length(min=, max=)The size of the annotated string must be within the specified range
@NotEmptyThe of the annotated string must be non empty
@Range(min=, max=)The annotated element must be within the appropriate range
@NotBlankThe of the annotated string must be non empty
@URL(protocol=,host=, port=, regexp=, flags=)The annotated string must be a valid url
@CreditCardNumberThe annotated string must pass the Luhn verification algorithm. Generally, Luhn is used to calculate the legitimacy of bank card, credit card and other numbers
@ScriptAssert(lang=, script=, alias=)There should be the implementation of Java Scripting API, namely JSR 223("Scripting for the JavaTM Platform")
@SafeHtml(whitelistType=,additionalTags=)There should be a jsoup package in the classpath

Message supports expressions and EL expressions, such as message = "name length is limited to {min} to {max} ${1+2}")

If you want to write the error description into properties, create a new validationmessages under the classpath_ zh_ CN. Properties file (note that value needs to be converted to unicode encoding), and then use placeholders in {} format

In the hibernate supplementary annotations, the last three are not commonly used and can be ignored.
The following three annotations @ notnull @ notempty @ notblank are distinguished:

  1. @NotNull the value of any object cannot be null
  2. @The element of the NotEmpty collection object is not 0, that is, the collection is not empty, or it can be used for strings that are not null
  3. @NotBlank can only be used when the string is not null, and the length after string trim() must be greater than 0

Group check

If the same parameter requires different verification rules to be applied in different scenarios, group verification is required. For example, if a newly registered user has not named a name, we allow the name field to be empty, but we do not allow the name to be updated to empty characters during update.

Group verification has three steps:

  1. Define a grouping class (or interface)
public interface Update extends Default{
}
  1. Add the groups attribute on the verification annotation to specify the groups
public class UserVO {
    @NotBlank(message = "name Cannot be empty",groups = Update.class)
    private String name;
    // Omit other code
}
  1. Add a grouping class with the @ Validated annotation of the Controller method
@PostMapping("update")
public ResultInfo update(@Validated({Update.class}) UserVO userVO) {
    return new ResultInfo().success(userVO);
}

The custom Update grouping interface inherits the Default interface. The verification annotation (e.g. @ NotBlank) and @ validated belong to Default Class grouping, which is in javax validation. groups. It is described in the Default note

/**
 * Default Jakarta Bean Validation group.
 * <p>
 * Unless a list of groups is explicitly defined:
 * <ul>
 *     <li>constraints belong to the {@code Default} group</li>
 *     <li>validation applies to the {@code Default} group</li>
 * </ul>
 * Most structural constraints should belong to the default group.
 *
 * @author Emmanuel Bernard
 */
public interface Default {
}

When writing the Update grouping interface, if Default is inherited, the following two methods are equivalent:
@Validated({Update.class}),@Validated({Update.class,Default.class})
If Update does not inherit Default, @ Validated({Update.class}) will only verify that it belongs to Update Class grouped parameter fields

Recursive check

If an attribute of the OrderVO class is added to the UserVO class and the attributes in the OrderVO also need to be verified, recursive verification is used, which can be achieved by adding the @ Valid annotation to the corresponding attribute (the same applies to the collection)

public class OrderVO {
    @NotNull
    private Long id;
    @NotBlank(message = "itemName Cannot be empty")
    private String itemName;
    // Omit other code
}
public class UserVO {
    @NotBlank(message = "name Cannot be empty",groups = Update.class)
    private String name;
    //OrderVO requiring recursive verification
    @Valid
    private OrderVO orderVO;
    // Omit other code
}   

Custom verification

Validation provides us with so many features that it can almost meet most parameter verification scenarios in daily development. However, a good framework must be easy to extend. With the expansion capability, you can cope with more complex business scenarios. After all, the only constant in the development process is the change itself. Validation allows users to customize validation

The implementation is simple in two steps:

  1. Custom verification annotation
package cn.soboys.core.validator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;


/**
 * @author kenx
 * @version 1.0
 * @date 2021/1/21 20:49 
 * Date validation constraint annotation class
 */
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {IsDateTimeValidator.class}) // Indicates which class performs the verification logic
public @interface IsDateTime {

    // The default message returned in case of verification error
    String message() default "Date format error";
    //Group check
    Class<?>[] groups() default {};

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

    //Here are the attributes I define myself
    boolean required() default true;

    String dateFormat() default "yyyy-MM-dd";


}

Note: message is used to display error messages. This field is required, as are groups and payload
@Constraint (validatedby = {handsomeboyvalidator. Class}) is used to specify the class that handles this annotation logic

  1. Write verifier class
package cn.soboys.core.validator;

import cn.hutool.core.util.StrUtil;

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

/**
 * @author kenx
 * @version 1.0
 * @date 2021/1/21 20:51
 * Date verifier
 */
public class IsDateTimeValidator implements ConstraintValidator<IsDateTime, String> {

    private boolean required = false;
    private String dateFormat = "yyyy-MM-dd";

    /**
     * Used to initialize the value on the annotation to this validator
     * @param constraintAnnotation
     */
    @Override
    public void initialize(IsDateTime constraintAnnotation) {
        required = constraintAnnotation.required();
        dateFormat = constraintAnnotation.dateFormat();
    }

    /**
     * Specific verification logic
     * @param value
     * @param context
     * @return
     */
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (required) {
            return ValidatorUtil.isDateTime(value, dateFormat);
        } else {
            if (StrUtil.isBlank(value)) {
                return true;
            } else {
                return ValidatorUtil.isDateTime(value, dateFormat);
            }
        }
    }

}

Note that I extracted the validation logic here and wrote a separate tool class, ValidatorUtil

package cn.soboys.core.validator;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author kenx
 * @version 1.0
 * @date 2021/1/21 20:51
 * Validation expression
 */
public class ValidatorUtil {
    private static final Pattern mobile_pattern = Pattern.compile("1\\d{10}");
    private static final Pattern money_pattern = Pattern.compile("^[0-9]+\\.?[0-9]{0,2}$");

    /**
     * Verify phone number
     *
     * @param src
     * @return
     */
    public static boolean isMobile(String src) {
        if (StrUtil.isBlank(src)) {
            return false;
        }
        Matcher m = mobile_pattern.matcher(src);
        return m.matches();
    }


    /**
     * Verify whether the enumeration value is legal. All enumerations need to inherit this method override
     *
     * @param beanClass Enumeration class
     * @param status    Corresponding code
     * @return
     * @throws Exception
     */
    public static boolean isEnum(Class<?> beanClass, String status) throws Exception {
        if (StrUtil.isBlank(status)) {
            return false;
        }

        //Conversion enumeration class
        Class<Enum> clazz = (Class<Enum>) beanClass;
        /**
         * Enumeration is actually a syntax sugar
         * Is a real column of multiple encapsulated Enum classes
         * Get all enumerated instances
         */
        Enum[] enumConstants = clazz.getEnumConstants();

        //Get method by method name
        Method getCode = clazz.getMethod("getCode");
        Method getDesc = clazz.getMethod("getDesc");
        for (Enum enums : enumConstants) {
            //Get enumeration instance name
            String instance = enums.name();
            //Execute the enumeration method to obtain the value corresponding to the enumeration instance
            String code = getCode.invoke(enums).toString();
            if (code.equals(status)) {
                return true;
            }
            String desc = getDesc.invoke(enums).toString();
            System.out.println(StrFormatter.format("Real column{}---code:{}desc{}", instance, code, desc));
        }
        return false;
    }

    /**
     * Verification amount 0.00
     *
     * @param money
     * @return
     */
    public static boolean isMoney(BigDecimal money) {
        if (StrUtil.isEmptyIfStr(money)) {
            return false;
        }
        if (!NumberUtil.isNumber(String.valueOf(money.doubleValue()))) {
            return false;
        }
        if (money.doubleValue() == 0) {
            return false;
        }
        Matcher m = money_pattern.matcher(String.valueOf(money.doubleValue()));
        return m.matches();
    }


    /**
     * Verification date
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static boolean isDateTime(String date, String dateFormat) {
        if (StrUtil.isBlank(date)) {
            return false;
        }
        try {
            DateUtil.parse(date, dateFormat);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}

I customized and supplemented many validators, including date verification, enumeration verification, mobile number verification and amount verification

The user-defined verification annotation is no different from the built-in annotation. You can add corresponding annotations to the required fields

Verification process analysis

The whole process of parameter validation using Validation API is shown in the figure below. The user accesses the interface and then performs parameter validation. If the validation passes, it enters the business logic. Otherwise, an exception is thrown and handed over to the global exception processor for processing

If the global exception comes out, please refer to my article Spring boot elegant global exception handling

Pay attention to official account apes and get more dry cargo sharing

Send parameter verification to obtain complete custom source code

Keywords: Spring Boot

Added by rockobop on Fri, 21 Jan 2022 03:57:57 +0200