gregg
gregg
JCHJava Community | Help. Code. Learn.
Created by gregg on 1/7/2025 in #java-help
annotation
Code: @Documented @Constraint(validatedBy = StringContainsValidator.class) @Target({ElementType.PARAMETER, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface StringContains { String message() default "字符串不符合规则"; String[] limitValues() default {}; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; } public class StringContainsValidator implements ConstraintValidator<StringContains, String> { private Set<String> limitValues; @Override public void initialize(StringContains constraintAnnotation) { limitValues = Arrays.stream(constraintAnnotation.limitValues()).collect(Collectors.toSet()); } @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (StrUtil.isBlank(value)) { return true; } return limitValues.contains(value.trim()); } } problem:How is the value in the message called when a string that does not meet the requirements is detected?
5 replies