创建字段校验工厂抽象接口
import java.lang.reflect.Field;
public abstract class AbstractAnnotationFactoty {
public AbstractAnnotationFactoty() {
}
//抽象方法
public abstract void beanValidate(Field field, Object bean) throws Exception;
}
声明邮箱校验继承上面的工厂类
import com.sula.frame.common.annotation.factory.AbstractAnnotationFactoty;
import com.sula.frame.common.annotation.utils.PatternUtil;
import com.sula.frame.common.annotation.utils.ValidatorUtil;
import com.sula.frame.common.annotation.validator.Email;
import com.sula.frame.common.exception.ServiceException;
import java.lang.reflect.Field;
import java.util.List;
public class EmailValidator extends AbstractAnnotationFactoty {
private static final String EMAIL = " 邮箱校验格式有误";
public EmailValidator() {
}
//实现抽象方法
public void beanValidate(Field field, Object bean) throws Exception {
this.check(field, bean, (List)null);
}
private void check(Field field, Object bean, List<String> excludes) throws Exception {
if (excludes == null || excludes.size() <= 0 || !excludes.contains(field.getName())) {
Object value = ValidatorUtil.getValue(field, bean);
if (!PatternUtil.patternEmail(value.toString())) {
Email email = (Email)field.getAnnotation(Email.class);
throw new ServiceException(email.value() + " 邮箱校验格式有误");
}
}
}
}
同上身份证校验
import com.sula.frame.common.annotation.factory.AbstractAnnotationFactoty;
import com.sula.frame.common.annotation.utils.PatternResult;
import com.sula.frame.common.annotation.utils.PatternUtil;
import com.sula.frame.common.annotation.utils.ValidatorUtil;
import com.sula.frame.common.annotation.validator.IDCard;
import com.sula.frame.common.exception.ServiceException;
import java.lang.reflect.Field;
import java.util.List;
public class IDCardValidator extends AbstractAnnotationFactoty {
private static final String IDCARD = "身份证号格式有误";
public IDCardValidator() {
}
public void beanValidate(Field field, Object bean) throws Exception {
this.check(field, bean, (List)null);
}
private void check(Field field, Object bean, List<String> excludes) throws Exception {
if (excludes == null || excludes.size() <= 0 || !excludes.contains(field.getName())) {
Object value = ValidatorUtil.getValue(field, bean);
PatternResult result = PatternUtil.regexIdCard(value.toString());
if (!result.isResult()) {
IDCard idCard = (IDCard)field.getAnnotation(IDCard.class);
throw new ServiceException(idCard.value() + "身份证号格式有误");
}
}
}
}
同上电话号码校验
import com.sula.frame.common.annotation.factory.AbstractAnnotationFactoty;
import com.sula.frame.common.annotation.utils.PatternUtil;
import com.sula.frame.common.annotation.utils.ValidatorUtil;
import com.sula.frame.common.annotation.validator.Tel;
import com.sula.frame.common.exception.ServiceException;
import java.lang.reflect.Field;
import java.util.List;
public class TelValidator extends AbstractAnnotationFactoty {
private static final String TEL = "座机号码格式有误";
public TelValidator() {
}
public void beanValidate(Field field, Object bean) throws Exception {
this.check(field, bean, (List)null);
}
private void check(Field field, Object bean, List<String> excludes) throws Exception {
if (excludes == null || excludes.size() <= 0 || !excludes.contains(field.getName())) {
Object value = ValidatorUtil.getValue(field, bean);
if (!PatternUtil.pattenTel(value.toString())) {
Tel tel = (Tel)field.getAnnotation(Tel.class);
throw new ServiceException(tel.value() + "座机号码格式有误");
}
}
}
}
同上数字格式校验
import com.sula.frame.common.annotation.factory.AbstractAnnotationFactoty;
import com.sula.frame.common.annotation.utils.PatternUtil;
import com.sula.frame.common.annotation.utils.ValidatorUtil;
import com.sula.frame.common.annotation.validator.Number;
import com.sula.frame.common.exception.ServiceException;
import java.lang.reflect.Field;
import java.util.List;
public class NumberValidator extends AbstractAnnotationFactoty {
private static final String NUMBER = "必须是数字";
public NumberValidator() {
}
public void beanValidate(Field field, Object bean) throws Exception {
this.check(field, bean, (List)null);
}
private void check(Field field, Object bean, List<String> excludes) throws Exception {
if (excludes == null || excludes.size() <= 0 || !excludes.contains(field.getName())) {
Object value = ValidatorUtil.getValue(field, bean);
if (!PatternUtil.patternNumber(value.toString())) {
Number number = (Number)field.getAnnotation(Number.class);
throw new ServiceException(number.value() + "必须是数字");
}
}
}
}
工厂类
import com.sula.frame.common.annotation.factory.validator.DefaultValidator;
import com.sula.frame.common.annotation.factory.validator.EmailValidator;
import com.sula.frame.common.annotation.factory.validator.IDCardValidator;
import com.sula.frame.common.annotation.factory.validator.MaxValidator;
import com.sula.frame.common.annotation.factory.validator.MinValidator;
import com.sula.frame.common.annotation.factory.validator.MobileValidator;
import com.sula.frame.common.annotation.factory.validator.NotEmptyValidator;
import com.sula.frame.common.annotation.factory.validator.NumberValidator;
import com.sula.frame.common.annotation.factory.validator.PatternValidator;
import com.sula.frame.common.annotation.factory.validator.TelValidator;
import com.sula.frame.common.annotation.validator.Email;
import com.sula.frame.common.annotation.validator.IDCard;
import com.sula.frame.common.annotation.validator.Max;
import com.sula.frame.common.annotation.validator.Min;
import com.sula.frame.common.annotation.validator.Mobile;
import com.sula.frame.common.annotation.validator.NotEmpty;
import com.sula.frame.common.annotation.validator.Number;
import com.sula.frame.common.annotation.validator.Pattern;
import com.sula.frame.common.annotation.validator.Tel;
import java.lang.reflect.Field;
public class BeanValidateFactory {
public BeanValidateFactory() {
}
public AbstractAnnotationFactoty getValidator(Field field) {
if (field.isAnnotationPresent(NotEmpty.class)) {
return new NotEmptyValidator();
} else if (field.isAnnotationPresent(IDCard.class)) {
return new IDCardValidator();
} else if (field.isAnnotationPresent(Email.class)) {
return new EmailValidator();
} else if (field.isAnnotationPresent(Tel.class)) {
return new TelValidator();
} else if (field.isAnnotationPresent(Mobile.class)) {
return new MobileValidator();
} else if (field.isAnnotationPresent(Number.class)) {
return new NumberValidator();
} else if (field.isAnnotationPresent(Pattern.class)) {
return new PatternValidator();
} else if (field.isAnnotationPresent(Min.class)) {
return new MinValidator();
} else {
return (AbstractAnnotationFactoty)(field.isAnnotationPresent(Max.class) ? new MaxValidator() : new DefaultValidator());
}
}
}
利用反射,获取对象实体上的注解,
import java.lang.reflect.Field;
public class BeanValidatorFactory {
public BeanValidatorFactory() {
}
public static void validate(Object bean) {
Class<?> clazz = bean.getClass();
Field[] fields = clazz.getDeclaredFields();
Field[] var6 = fields;
int var5 = fields.length;
for(int var4 = 0; var4 < var5; ++var4) {
Field field = var6[var4];
//初始化工厂类方法的实现为抽象类
AbstractAnnotationFactoty annotationFactoty = (new BeanValidateFactory()).getValidator(field);
try {
annotationFactoty.beanValidate(field, bean);
} catch (Exception var9) {
var9.printStackTrace();
}
}
}
}
使用字段格式校验
BeanValidatorFactory.validate(user);