Spring custom verification

Reference material

JSR303, 349 -Bean Validation data validation specification instructions and validation process source code analysis
Spring global exception handling

Matters needing attention

  1. Use Java JSR303, 349 Bean Validation data validation specification to do data validation reference: JSR303, 349 -Bean Validation data validation specification instructions and validation process source code analysis
  2. Make good use of grouping to reduce the logic code of verification
  3. The grouped class needs an interface. We can define a package to store it

  1. Configure the global exception unified processing, so that the business exception and verification exception can be handled uniformly, and the code logic can be liberated


The MobileValidator verification class is as follows:

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

public class MobileValidator implements ConstraintValidator<Mobile, String> {

    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        System.out.println("Mobile The mobile number is:" + s);
        return true;

The notes are as follows:

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.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Constraint(validatedBy = MobileValidator.class)
public @interface Mobile {
    String message() default "Incorrect phone number format";

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

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


public class MainController {

    public String login(@Validated(value = LoginGroup.class) UserAO userAO) {
        log.info("userAO:" + JSON.toJSONString(userAO));
        return JSON.toJSONString(userAO);

    public String forget(@Validated(value = ForgetPasswordGroup.class) UserAO userAO) {
        log.info("userAO:" + JSON.toJSONString(userAO));
        return JSON.toJSONString(userAO);

Verified entity class UserAO

public class UserAO {

    @NotBlank(message = "User name cannot be empty", groups = LoginGroup.class)
    private String username;

    @NotBlank(message = "Password cannot be empty when logging in", groups = LoginGroup.class)
    @NotBlank(message = "Old password cannot be empty when forgetting password", groups = ForgetPasswordGroup.class)
    private String password;

    @NotBlank(message = "The new password cannot be empty when the password is forgotten", groups = ForgetPasswordGroup.class)
    private String newPassword;



Spring implements the support of javax verification framework. We use javax verification framework to implement parameter verification, which can reduce a lot of if type verification code, make the development focus on business logic as much as possible, and make the code logic clearer.
Generally speaking, the development efficiency is improved, the code is simplified and easy to read.
1, Do you have any understanding of JSR? You need to read relevant JSE, J2EE's JSR regulations and address

  1. JAVA EE https://www.jcp.org/en/jsr/platform?listBy=3&listByType=platform
  2. JAVA SE https://www.jcp.org/en/jsr/platform?listBy=2&listByType=platform
  3. JAVA ME https://www.jcp.org/en/jsr/platform?listBy=1&listByType=platform

2, I don't know how Spring implements this verification framework and whether there are other extensions and supports

Requirements: understand the implementation principle of the calibration framework

3, How to implement the custom verification of Spring verification?

Published 33 original articles, praised 0, visited 101
Private letter follow

Tags: Java Mobile JSON Bean Validation

Posted on Mon, 20 Jan 2020 02:47:33 -0500 by perrij3