Home  >  Article  >  Java  >  How to customize verification in Springboot

How to customize verification in Springboot

PHPz
PHPzforward
2023-05-10 16:13:06935browse

StartWithValidation.class

@Documented
@Constraint(validatedBy = StartWithValidator.class )
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface StartWithValidation {
  String message() default "不是正确的性别取值范围";
  String start() default "_";
  Class<?>[] groups() default {};
  Class<? extends Payload>[] payload() default {};
  @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
  @Retention(RUNTIME)
  @Documented
  @interface List {
    StartWithValidation[] value();
  }
}

StartWithValidator.class

public class StartWithValidator implements ConstraintValidator<StartWithValidation, String> {
  private String start;
  @Override
  public void initialize(StartWithValidation constraintAnnotation) {
    start = constraintAnnotation.start();
  }

  @Override
  public boolean isValid(String value, ConstraintValidatorContext context) {

    if (!StringUtils.isEmpty(value)) {
      return value.startsWith(start);
    }
    return true;
  }
}

运用

@StartWithValidation(message = "Param 'address' must be start with 'Beijing'.", start = "Beijing")
public String address;

The above is the detailed content of How to customize verification in Springboot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete