代码已更新
API层面(拦截器):
Override
public void intercept(Invocation inv) {
Method method = inv.getMethod();
Validated annotation = method.getAnnotation(Validated.class);
if (annotation == null) {
annotation = inv.getTarget().getClass().getAnnotation(Validated.class);
}
if (annotation != null) {
/**得到参数*/
Parameter[] parameters = method.getParameters();
if (parameters != null && ArrayUtil.isNotEmpty(parameters)) {
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
if (parameter != null) {
/**得到参数注解*/
Annotation[] annotations = parameter.getAnnotations();
if (annotations != null) {
for (Annotation parameterAnnotation : annotations) {
if (parameterAnnotation != null) {
if (MapUtil.isNotEmpty(ValidateAbstract.validates)) {
Set<Class<?>> classes = ValidateAbstract.validates.keySet();
/**注解是否符合指定校验类型*/
if (ValidateAbstract.validates.containsKey(parameterAnnotation.annotationType())) {
/**得到校验类*/
ValidateAbstract iValidate = ValidateAbstract.validates.get(parameterAnnotation.annotationType());
/**校验是否符合规则*/
if (iValidate.supports(method, parameter, parameterAnnotation.annotationType())) {
/**校验*/
Result verify = iValidate.verify(parameterAnnotation, inv.getArg(i), method, parameter);
/**code != 0 为失败 */
if (verify.getCode() != 0) {
inv.getController().renderJson(verify);
return;
}
}
}
}
}
}
}
}
}
}
}
inv.invoke();
}
真正做验证的验证器
public abstract class ValidateAbstract<T extends Annotation> implements IValidate<T>{
protected Boolean isSupports = false;
protected T annotation;
/**二级验证支持子类实现 */
protected abstract boolean supportsParameter(Parameter parameter,Class<T> t);
/**子类*/
public static Map<Class<?>, ValidateAbstract> validates = new HashMap<>();
/**一级验证支持*/
@Override
public boolean supports(Method method, Parameter parameter,Class<T> t) {
this.isSupports = method != null && (method.getAnnotation(Validated.class) != null
?true:method.getDeclaringClass().getAnnotation(Validated.class)!= null);
if(this.isSupports){
this.isSupports = supportsParameter(parameter,t);
}
return isSupports;
}
/** 验证 */
@Override
public Result verify(T t,Object o, Method method, Parameter parameter) {
if(isSupports){
if(method==null || parameter == null){
throw new ValidateException("错误的调用:hehh.core.validation.verifier.ValidateAbstract Method verify("+t.getClass()+",java.lang.Object,java.lang.reflect.Method,java.lang.reflect.Parameter) method Parameter not null");
}
this.annotation = t;
return verifyParameter(o,method,parameter);
}
throw new ValidateException("错误的调用:hehh.core.validation.verifier.ValidateAbstract 不支持验证.:"+method.getName()+" "+parameter.getName());
}
/** 真正验证的方法 */
protected abstract Result verifyParameter(Object o, Method method, Parameter parameter);
}
现已实现常用的验证(包括service层的)有:
AssertFalse 不为null时必须是false
AssertTrue 不为null时必须是true
Digits 被注释的元素必须是一个数字,其值必须在可接受的范围内
* 且整数部分的位数不能超过integer,小数部分的位数不能超过fraction
Future 被注释的元素必须是一个将来的日期 type1 :毫秒、2:秒、3:分、4:时 、5:天
6:周、7:月、8:年
Past 必须是将来的日期
Max 最大值
Min 最小值
NotBlank 字符串不为空
NotEmpty 数组 集合 不为空
NotNull 不允许为null
Size 验证长度 字符串 数组 集合
Pattern 匹配指定正则
在项目启动时,获取 ValidateAbstract 子类放入静态变量中,
在拦截器中根据使用的注解去获取对应的验证器,然后进行验证。 拦截器代码同样适合验证service方法
地址:https://gitee.com/heHouHui/jfinal_validated.git
我不生成代码,我只是代码的搬运工