Home > Java > Java Tutorial > body text

How to customize verification in Springboot

PHPz
Release: 2023-05-10 16:13:06
forward
814 people have browsed it

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[] payload() default {};
  @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
  @Retention(RUNTIME)
  @Documented
  @interface List {
    StartWithValidation[] value();
  }
}
Copy after login

StartWithValidator.class

public class StartWithValidator implements ConstraintValidator {
  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;
  }
}
Copy after login

运用

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

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!