使用方法:
ValidatedType.java //注解验证类型
Validateds.java //注解
Validated.java //注解
ValidatedInterceptor.java //全局拦截判断
//1. 全局配置里增加 @Override public void configInterceptor(Interceptors me) { me.add(new ValidatedInterceptor()); }
//2. Controller 加入 @Validated(field = "username", type = ValidatedType.isNotBlank, errorMessage = "用户名不能为空") @Validated(field = "password", errorMessage = "密码不能为空") @Validated(field = "age",type = ValidatedType.isInteger, errorMessage = "输入的年龄有误") @Validated(field = "email", type = ValidatedType.isEmail, errorMessage = "输入的邮箱有误") public void doLogin( @Para("username") String username, @Para("password") String password, @Para("age") Integer age, @Para("email") String email ) { //登陆过程.... renderJson(Ret.ok()); }
package app.interceptor; public enum ValidatedType { isNotBlank, isEmail, isInteger }
package app.interceptor; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Inherited @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) public @interface Validateds { Validated[] value(); }
package app.interceptor; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Inherited @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @Repeatable(Validateds.class) public @interface Validated { String field() default ""; ValidatedType type() default ValidatedType.isNotBlank; String errorMessage() default ""; }
package app.interceptor; import com.jfinal.aop.Interceptor; import com.jfinal.aop.Invocation; import com.jfinal.core.Controller; import com.jfinal.kit.Kv; import com.jfinal.kit.Ret; import com.jfinal.kit.StrKit; public class ValidatedInterceptor implements Interceptor { static final String regexInteger = "\\d+"; static final String regexEmail = "\\b(^['_A-Za-z0-9-]+(\\.['_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z0-9]{2,})|(\\.[A-Za-z0-9]{2,}\\.[A-Za-z0-9]{2,}))$)\\b"; @Override public void intercept(Invocation inv) { Validateds inputVerifys = getAnnotation(inv); if (inputVerifys == null) { inv.invoke(); return; } doVerifys(inv, inputVerifys); } private Validateds getAnnotation(Invocation inv) { Validateds inputVerifys = inv.getController().getClass().getAnnotation(Validateds.class); return inputVerifys != null ? inputVerifys : inv.getMethod().getAnnotation(Validateds.class); } private void doVerifys(Invocation inv, Validateds inputVerifys) { Controller c = inv.getController(); Kv kv = new Kv(); if (inputVerifys != null) { for (Validated input : inputVerifys.value()) { if (isValidation(input.type(), c.getPara(input.field()))) { kv.set(input.field(), input.errorMessage()); } } } if (!kv.isEmpty()) { c.renderJson(Ret.data(kv).setFail()); } else { inv.invoke(); return; } } private boolean isValidation(ValidatedType type, Object value) { switch (type) { case isNotBlank: return (value == null ? true : StrKit.isBlank(value.toString())); case isInteger: return (value == null ? true : !value.toString().matches(regexInteger)); case isEmail: return (value == null ? true : !value.toString().matches(regexEmail)); default: return (value == null ? true : StrKit.isBlank(value.toString())); } } }
代码参照 EnableCORS 写的, 希望@JFinal能细化代码,并收入jfinal jar里面. 方便使用者验证.