javax.validation 自定义校验器

Posted jis117

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javax.validation 自定义校验器相关的知识,希望对你有一定的参考价值。

import java.util.Iterator;
import java.util.List;

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

import org.apache.commons.lang3.StringUtils;

public class ListStringPatternValidator
        implements ConstraintValidator<ListStringPattern, List<String>> {
    private String pattern;
    private String message;

    public void initialize(ListStringPattern constraintAnnotation) {
        this.pattern = constraintAnnotation.regexp();
        this.message = constraintAnnotation.message();
    }

    public boolean isValid(List<String> list, ConstraintValidatorContext constraintContext) {
        if (list == null) {
            return true;
        }
        String errorString = null;
        boolean isValid = true;
        if (pattern == null || "".equals(pattern.trim())) {
            isValid = false;
            if (!isValid) {
                constraintContext.disableDefaultConstraintViolation();
                constraintContext.buildConstraintViolationWithTemplate("指定的正则表达式为空")
                        .addConstraintViolation();
            }
            return isValid;
        } else {
            Iterator<String> iter = list.iterator();
            while (iter.hasNext()) {
                String string = iter.next();
                if (!string.matches(pattern)) {
                    isValid = false;
                    errorString = string;
                    break;
                }
            }
        }
        if (!isValid) {
            constraintContext.disableDefaultConstraintViolation();
            String message = this.message;
            if (StringUtils.isBlank(message)) {
                message = "List<String>为空,或者其中的" + errorString + "不符合正则表达式:" + pattern;;
            }
            constraintContext.buildConstraintViolationWithTemplate(message).addConstraintViolation();
        }
        return isValid;
    }

}
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.*;

import javax.validation.Constraint;
import javax.validation.Payload;

/**
 * 自定义校验器
 * 校验List<String>中的参数
 * <pre>
 * @ListStringPattern(regexp = "", message = "")
 * </pre>
 * @author rong.e
 *
 */
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ListStringPatternValidator.class)
@Documented
public @interface ListStringPattern {

    String message() default "string in list no match pattern";

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

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

    String regexp();

    /**
     * Defines several {@link ListStringPattern} annotations on the same element.
     * 
     * @see ListStringPattern
     */
    @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @interface List {
        ListStringPattern[] value();
    }

}

 

以上是关于javax.validation 自定义校验器的主要内容,如果未能解决你的问题,请参考以下文章

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 解决方法(

spring请求参数校验

springboot 实体参数校验 validate 抛出javax.validation.ConstraintViolationException异常 统一处理

SpringBoot实现数据格式校验

write RE validation

spring boot中使用javax.validation以及org.hibernate.validator校验入参