标签: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(); } }
标签:boolean oid not link void ace text public retention
原文地址:http://www.cnblogs.com/jpit/p/7474938.html