码迷,mamicode.com
首页 > 编程语言 > 详细

javax.validation 自定义校验器

时间:2017-09-04 18:59:36      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:boolean   oid   not   link   void   ace   text   public   retention   

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 自定义校验器

标签:boolean   oid   not   link   void   ace   text   public   retention   

原文地址:http://www.cnblogs.com/jpit/p/7474938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!