标签:size 最小 lan 问题 初始化 sage ext .text cep
由于工作中对接第三方公司借口需要对字段做很多校验,所以使用到了下面的自定义注解,在这里记录一下;
空指针校验:
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; @Documented @Inherited @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface ISEmptyAnnotation { public boolean isEmpty() default true; public String message() default "字段不能为空!"; }
最大长度校验:
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; @Documented @Inherited @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface MaxSize { public int max() default 20; public String message() deault "长度太长"; }
最小长度校验:
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; @Documented @Inherited @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface MinSize { public int min() default 0; public String message() deault "长度太短"; }
时间格式校验:
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; @Documented @Inherited @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface DateFormat{ public String format() default "YYYY-MM-DD"; public String message() deault "时间格式类型有误!"; }
注解验证处理:
import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Map; public class AnnotationDealUtil { public static Map<String, Object> validate(Object bean) { Map<String, Object> result = new HashMap<String, Object>(); result.put("message","验证通过"); result.put("result", true); Class<?> cls = bean.getClass(); // 检测field是否存在 try { // 获取实体字段集合 Field[] fields = cls.getDeclaredFields(); for(Field f : fields) { // 通过反射获取该属性对应的值 f.setAccessible(true); // 获取字段值 Object value = f.get(bean); Annotation[] arrayAnno = f.getAnnotations(); for(Annotation annotation : arrayAnno) { // 获取注解类型(注解类的Class) Class<?> clazz = annotation.annotationType(); // 获取注解类中的方法集合 Method[] methodArray = clazz.getDeclaredMethods(); for (Method method : methodArray) { // 获取方法名 String methodName = method.getName(); // 过滤错误提示方法的调用 if(methodName.equals("message")) { continue; } // 初始化注解验证方法处理类 Object obj= AnnotationDealUtil.class.newInstance(); // 获取方法 try { // 根据方法名获取该方法 Method m = obj.getClass().getDeclaredMethod(methodName, Object.class, Field.class); // 调用该fangfa result = (Map(String, Object>) m.invoke(obj,value, f); // 验证结果 有失败就则退出 if(result.get("result").equals(false)) { return result; } } catch (Exception e) { e.printStackTrace(); } } } } } catch (Exception e) { e.printStackTrace(); } return result; } /** * 验证是否空值 * */ public Map<String, Object> isEmpty(Object value, Field field) { Map<String, Object> validateResult = new HashMap<String, Object>(); IsEmptyAnnotation annotation = field.getAnnotation(IsEmptyAnnotation.class); if(value == null || value.equals("")) { validateResult.put("message", field.getName() + annotation.getMessage()); validateResult.put("result", false); } else { validateResult.put("message", "验证通过"); validateResult.put("result", true); } return validateResult; } /** * 验证最小 * */ public Map<String, Object> min(Object value, Field field) { Map<String, Object> validateResult = new HashMap<String, Object>(); MinSize annotation = field.getAnnotation(MinSize.class); if(value != null && value.toString.length() < annotation.min()){ validateResult.put("message", annotation.message()); validateResult.put("result", false); } else { validateResult.put("message", "验证通过"); validateResult.put("result", true); } return validateResult; } /** * 验证最大长度 * */ public Map<String, Object> max(Object value, Field field) { Map<String, Object> validateResult = new HashMap<String, Object>(); MaxSize annotation = field.getAnnotation(MaxSize.class); if(value != null && value.toString.length() < annotation.min()){ validateResult.put("message", annotation.message()); validateResult.put("result", false); } else { validateResult.put("message", "验证通过"); validateResult.put("result", true); } return validateResult; } /** * 验证日期格式 * */ public Map<String, Object> max(Object value, Field field) { Map<String, Object> validateResult = new HashMap<String, Object>(); DateFormat annotation = field.getAnnotation(DateFormat.class); if(value != null && annotation.format() != null){ validateResult.put("message", "验证通过"); validateResult.put("result", true); SimpleDateFormat dateFormat = new SimpleDateFormat(annotation.format()); try { dateFormat.setLenient(false); dateFormat.format(value); } catch (Exception e) { validateResult.put("message", annotation.message()); validateResult.put("result", false); } } else { validateResult.put("message", "验证通过"); validateResult.put("result", true); } return validateResult; } }
使用:
由于代码在虚拟机中,复制不出来,纯手打,有问题之处请多多指出!
标签:size 最小 lan 问题 初始化 sage ext .text cep
原文地址:https://www.cnblogs.com/greatdynasty/p/10348136.html