自定义注解校验工具类,大大提高开发效率

 

各位读者,大家好!

    今天我给大家带来一份每个开发者必须面对的工作——参数校验,比如非空校验、长度校验、取值范围、格式等等。

    如果我们每次都写if...else...代码,仔细总结下来,好像代码都前篇一律,对自己的能力并没有什么提升。那么,你需要的人性化校验工具,他来了!

    为什么人性化,是因为其校验及其简单,还可以将校验结果输出给客户,一眼就知道哪个参数传值有问题,大大节省了我们运维的时间和劳力。

    话不多说,直接上代码:

 

整体包结构如下图

 

1) 首先,自定义一个枚举类,定义我们需要输出的code和message,用于给客户或者被调用者以友好的方式提示。即便是非开发人员也可以通俗易懂的理解

 1 package com.cheng2839.enums;
 2 
 3 /**
 4  * 字段长度校验注解
 5  *
 6  * @author cheng2839
 7  * @date 2018年11月16日
 8  */
 9 public enum ErrorEnum {
10 
11     /*
12      * 参数校验注解默认提示
13      */
14     PARAM_FIELD_NULL("10001", "必输字段不能为空!"),
15     PARAM_FIELD_NULL_OR("10002", "所选字段不能全部为空!"),
16     PARAM_FIELD_LEN("10003", "必输长度不符合要求!"),
17     PARAM_FIELD_NUMBER("10004", "数字大小不符合要求!"),
18     PARAM_FIELD_VALUE("10005", "字段取值不正确!"),
19 
20 
21 
22 
23     PARAM_LOGINACCOUNT_NOTNULL("3001", "登录账户不能为空!"),
24     PARAM_LOGINACCOUNT_LEN("3002", "登录账户只能为3至8位!"),
25     PARAM_LOGINPWD_NOTNULL("3003", "登录密码不能为空!"),
26     PARAM_LOGINPWD_LEN("3004", "登录密码不能超过20位!"),
27     PARAM_LOGINTYPE_NOTNULL("3005", "登录类型不能为空!"),
28     PARAM_LOGINTYPE_VALUE("3006", "登录类型取值不正确!"),
29     PARAM_CMSCHECKCODE_NOTNULL("3007", "短信验证码不能为空!"),
30     PARAM_CMSCHECKCODE_NUMBER("3008", "短信验证码为6位!"),
31     PARAM_PHONE_EMAIL_OR("3009", "电话和邮箱其一必填!"),
32     ;
33 
34 
35     private String msg;
36 
37     private String code;
38 
39     private ErrorEnum(String code, String msg) {
40         this.code = code;
41         this.msg = msg;
42     }
43 
44     public String getMsg() {
45         return this.msg;
46     }
47 
48     public String getCode() {
49         return this.code;
50     }
51 }

 

2)定义我们需要校验的注解,这里我定义了最常用的5种:非空(非null,""," ")、不全为空(多个参数至少有一个不为空)、值长度(特别是入库需要判断,否则会报错)、最大最小值(数字、金额类)、取值集合(比如系统来源、接口类型等)

 

FieldLength 长度校验的注解

 1 package com.cheng2839.annotation;
 2 
 3 import com.cheng2839.enums.ErrorEnum;
 4 
 5 import java.lang.annotation.Documented;
 6 import java.lang.annotation.ElementType;
 7 import java.lang.annotation.Retention;
 8 import java.lang.annotation.RetentionPolicy;
 9 import java.lang.annotation.Target;
10 
11 /**
12  * 字段长度校验注解
13  *
14  * @author cheng2839
15  * @date 2018年11月16日
16  */
17 @Retention(RetentionPolicy.RUNTIME)
18 @Target(ElementType.FIELD)
19 @Documented
20 public @interface FieldLength {
21 
22     // 字段长度最大值 - string类型
23     int maxLen() default Integer.MAX_VALUE;
24 
25     // 字段长度最大值 - string类型
26     int minLen() default 0;
27 
28     //提示信息
29     String msg() default "field length is too long or too short.";
30 
31     ErrorEnum msgEnum() default ErrorEnum.PARAM_FIELD_LEN;
32 }
 1 package com.cheng2839.annotation;
 2 
 3 import com.cheng2839.enums.ErrorEnum;
 4 
 5 import java.lang.annotation.Documented;
 6 import java.lang.annotation.ElementType;
 7 import java.lang.annotation.Retention;
 8 import java.lang.annotation.RetentionPolicy;
 9 import java.lang.annotation.Target;
10 
11 /**
12  * 字段非空校验注解
13  *
14  * @author cheng2839
15  * @date 2018年11月16日
16  */
17 @Retention(RetentionPolicy.RUNTIME)
18 @Target(ElementType.FIELD)
19 @Documented
20 public @interface FieldNotNull {
21 
22     //提示信息
23     String msg() default "field value is not empty.";
24 
25     ErrorEnum msgEnum() default ErrorEnum.PARAM_FIELD_NULL;
26 }
 1 package com.cheng2839.annotation;
 2 
 3 import com.cheng2839.enums.ErrorEnum;
 4 
 5 import java.lang.annotation.Documented;
 6 import java.lang.annotation.ElementType;
 7 import java.lang.annotation.Retention;
 8 import java.lang.annotation.RetentionPolicy;
 9 import java.lang.annotation.Target;
10 
11 /**
12  * 字段非空或校验注解
13  *
14  * @author cheng2839
15  * @date 2018年11月16日
16  */
17 @Retention(RetentionPolicy.RUNTIME)
18 @Target(ElementType.FIELD)
19 @Documented
20 public @interface FieldNotNullOr {
21 
22     //提示信息
23     String msg() default "field value is not simultaneously empty.";
24 
25     ErrorEnum msgEnum() default ErrorEnum.PARAM_FIELD_NULL_OR;
26 
27 }
 1 package com.cheng2839.annotation;
 2 
 3 import com.cheng2839.enums.ErrorEnum;
 4 
 5 import java.lang.annotation.Documented;
 6 import java.lang.annotation.ElementType;
 7 import java.lang.annotation.Retention;
 8 import java.lang.annotation.RetentionPolicy;
 9 import java.lang.annotation.Target;
10 
11 /**
12  * 字段值大小校验注解
13  *
14  * @author cheng2839
15  * @date 2018年11月16日
16  */
17 @Retention(RetentionPolicy.RUNTIME)
18 @Target(ElementType.FIELD)
19 @Documented
20 public @interface FieldNumber {
21 
22     // 数字可取最大值
23     double max() default Double.MAX_VALUE;
24 
25     // 数字可取值最小值
26     double min() default 0;
27 
28     //提示信息
29     String msg() default "number is too big or too small.";
30 
31     ErrorEnum msgEnum() default ErrorEnum.PARAM_FIELD_NUMBER;
32 
33 }
 1 package com.cheng2839.annotation;
 2 
 3 import com.cheng2839.enums.ErrorEnum;
 4 
 5 import java.lang.annotation.Documented;
 6 import java.lang.annotation.ElementType;
 7 import java.lang.annotation.Retention;
 8 import java.lang.annotation.RetentionPolicy;
 9 import java.lang.annotation.Target;
10 
11 /**
12  * 字段值枚举校验注解
13  *
14  * @author cheng2839
15  * @date 2018年11月16日
16  */
17 @Retention(RetentionPolicy.RUNTIME)
18 @Target(ElementType.FIELD)
19 @Documented
20 public @interface FieldValue {
21 
22     // 字段取值范围-可枚举
23     String[] values() default {};
24 
25     //提示信息
26     String msg() default "field value is incorrect.";
27 
28     ErrorEnum msgEnum() default ErrorEnum.PARAM_FIELD_VALUE;
29 
30 }

3)定义校验返回包装类,可以输出任何信息,比如被校验住的信息提示、错误码、错误字段名、错误字段的值等,以便我们开发人员能够轻松定位问题

 1 package com.cheng2839.model;
 2 
 3 import com.cheng2839.enums.ErrorEnum;
 4 
 5 /**
 6  * 参数校验返回结果
 7  *
 8  * @author cheng2839
 9  * @date 2018年11月16日
10  */
11 public final class ParamCheckResult {
12 
13     //校验结果
14     private boolean isPass = true;
15     //被校验不通过的字段名
16     private String fieldName;
17     //  被校验不通过的字段值
18     private Object value;
19     //被校验不通过的提示信息
20     private String msg;
21     //ThirdErrorEnum类型提示信息
22     private ErrorEnum errorEnum;
23     private String msgEnum;
24     private String msgCode;
25 
26     public ParamCheckResult(String msg) {
27         this.isPass = true;
28         this.msg = msg;
29     }
30 
31     public ParamCheckResult(String fieldName, Object value, String msg) {
32         passFail(fieldName, value, msg);
33         this.msg = msg;
34     }
35 
36     public ParamCheckResult(ErrorEnum msgEnum) {
37         passFail(null, null, null);
38         setMsgEnum(msgEnum);
39     }
40 
41     public ParamCheckResult(String fieldName, Object value, ErrorEnum msgEnum) {
42         passFail(fieldName, value, null);
43         setMsgEnum(msgEnum);
44     }
45 
46     public void setMsgEnum(ErrorEnum msgEnum) {
47         this.errorEnum = msgEnum;
48         this.msgEnum = msgEnum.getMsg();
49         this.msgCode = msgEnum.getCode();
50     }
51 
52     public ErrorEnum getEnum() {
53         return this.errorEnum;
54     }
55 
56     private void passFail(String fieldName, Object value, String msg) {
57         this.isPass = false;
58         this.fieldName = fieldName;
59         this.value = value;
60         this.msg = msg;
61     }
62 
63     public boolean isPass() {
64         return isPass;
65     }
66 
67     public boolean isNotPass() {
68         return !isPass();
69     }
70 
71       /* getter functions */72 }

 

4)校验工具类,最重要的环节。通过java的反射机制实现,注意,因为源始代码中依赖了部分springframework的工具类,比较方便,但是为了大家能够直接使用,我做了去依赖,对功能不会产生任何影响。

  1 package com.cheng2839.utils;
  2 
  3 import com.cheng2839.annotation.FieldLength;
  4 import com.cheng2839.annotation.FieldNotNull;
  5 import com.cheng2839.annotation.FieldNotNullOr;
  6 import com.cheng2839.annotation.FieldNumber;
  7 import com.cheng2839.annotation.FieldValue;
  8 import com.cheng2839.enums.ErrorEnum;
  9 import com.cheng2839.model.ParamCheckResult;
 10 
 11 import java.lang.annotation.Annotation;
 12 import java.lang.reflect.Field;
 13 import java.lang.reflect.InvocationTargetException;
 14 import java.lang.reflect.Method;
 15 import java.math.BigDecimal;
 16 import java.util.ArrayList;
 17 import java.util.Arrays;
 18 import java.util.HashMap;
 19 import java.util.List;
 20 import java.util.Map;
 21 
 22 /**
 23  * 参数校验工具类
 24  *
 25  * <p>
 26  * <title>使用方法</title>
 27  * 1)创建bean(可继承)
 28  * ParamBean pbean = o;
 29  * 2)在字段上根据需要使用注解
 30  * <li>@FieldNotNull</li>
 31  * <li>@FieldNotNullOr</li>
 32  * <li>@FieldValue</li>
 33  * <li>@FieldNumber</li>
 34  * <li>@FieldLength</li>
 35  * 3)使用校验类
 36  * ParamCheckResult checkRs = ParamCheckUtils.annotationCheck(pbean);
 37  * boolean isPass = checkRs.isPass();
 38  * if (!isPass) then exception handle...
 39  * <title>注意:</title>
 40  * 1)字段只可单继承,即字类含有的字段,在父类中无效,字段值为get方法的值;
 41  * </p>
 42  *
 43  * @author cheng2839
 44  * @date 2018年11月16日
 45  */
 46 public class ParamCheckUtils {
 47 
 48     private static final String CHECK_ERROR = "ERROR:参数校验异常,系统出错!";
 49 
 50     private ParamCheckUtils() {
 51     }
 52 
 53     /**
 54      * 批量校验字符串长度
 55      *
 56      * @param params
 57      * @return
 58      * @author cheng2839
 59      * @date 2018年11月16日
 60      */
 61     public static String strLengthCheckBat(Map<String, Integer> params) {
 62         if (params!=null) {
 63             for (Map.Entry<String, Integer> entry : params.entrySet()) {
 64                 String val = entry.getKey();
 65                 if (!strLengthCheck(val, entry.getValue())) {
 66                     return val;
 67                 }
 68             }
 69         }
 70         return null;
 71     }
 72 
 73     /**
 74      * 字符串长度校验
 75      *
 76      * @param val    被校验值
 77      * @param maxLen 最大长度
 78      * @return
 79      * @author cheng2839
 80      * @date 2018年11月16日
 81      */
 82     public static boolean strLengthCheck(String val, int maxLen) {
 83         return (val == null) || (val.length() <= maxLen);
 84     }
 85 
 86     /**
 87      * 数字范围校验
 88      *
 89      * @param val 被校验值
 90      * @param min 最小值
 91      * @param max 最大值
 92      * @return
 93      * @author cheng2839
 94      * @date 2018年11月16日
 95      */
 96     public static boolean numberCheck(BigDecimal val, BigDecimal min, BigDecimal max) {
 97         return (val == null) || (val.compareTo(min) >= 0 && val.compareTo(max) <= 0);
 98     }
 99 
100     /**
101      * 结合注解校验对象
102      *
103      * @param o
104      * @return
105      * @author cheng2839
106      * @date 2018年11月16日
107      */
108     public static ParamCheckResult annotationCheck(Object o) throws Exception {
109         if (o == null) {
110             throw new Exception("checked object is null.");
111         }
112 
113         ParamCheckResult checkResult = null; //校验返回结果
114         boolean isPass = true; //校验结果
115         ErrorEnum msgEnum = null; //校验提示信息
116         Map<String, Object> notNullAllMap = new HashMap<>(); //异或为空字段-值集合
117         ErrorEnum notAllNullMsgEnum = null; //异或提示信息
118         try {
119             //获取所有字段,不重复[遵循字类继承父类]
120             Field[] fields = getAllDeclaredFieldsNoRepeat(o.getClass());
121             for (Field f : fields) {
122                 f.setAccessible(true);
123                 if (isPass && hasAnnotation(f)) {
124                     Object val = getValue(f, o);
125                     if (isPass && f.isAnnotationPresent(FieldNotNull.class)) {
126                         isPass = notNull(val);
127                         msgEnum = f.getDeclaredAnnotation(FieldNotNull.class).msgEnum();
128                     }
129                     if (isPass && f.isAnnotationPresent(FieldValue.class)) {
130                         FieldValue fv = f.getDeclaredAnnotation(FieldValue.class);
131                         isPass = values(val, fv.values());
132                         msgEnum = fv.msgEnum();
133                     }
134                     if (isPass && f.isAnnotationPresent(FieldLength.class)) {
135                         FieldLength fl = f.getDeclaredAnnotation(FieldLength.class);
136                         isPass = length(val, fl.minLen(), fl.maxLen());
137                         msgEnum = fl.msgEnum();
138                     }
139                     if (isPass && f.isAnnotationPresent(FieldNumber.class)) {
140                         FieldNumber fn = f.getDeclaredAnnotation(FieldNumber.class);
141                         isPass = number(val, fn.min(), fn.max());
142                         msgEnum = fn.msgEnum();
143                     }
144                     if (isPass && f.isAnnotationPresent(FieldNotNullOr.class)) {
145                         notNullAllMap.put(f.getName(), val);
146                         notAllNullMsgEnum = f.getDeclaredAnnotation(FieldNotNullOr.class).msgEnum();
147                     }
148                     checkResult = new ParamCheckResult(f.getName(), val, msgEnum);
149                 }
150             }
151 
152             //异或非空集合校验
153             if (isPass && notNullAllMap.size() > 0) {
154                 isPass = false;
155                 for (Object v : notNullAllMap.values()) {
156                     if (notNull(v)) {
157                         isPass = true;
158                         break;
159                     }
160                 }
161                 if (!isPass) {
162                     checkResult = new ParamCheckResult(null, null, notAllNullMsgEnum);
163                 }
164             }
165 
166             if (isPass) {
167                 checkResult = new ParamCheckResult("check all pass!");
168             }
169         } catch (IllegalAccessException iae) {
170             checkResult = new ParamCheckResult("", "", CHECK_ERROR);
171         } catch (InvocationTargetException ite) {
172             checkResult = new ParamCheckResult(null, null, CHECK_ERROR);
173         }
174 
175         return checkResult;
176     }
177 
178     /**
179      * 校验是否存在注解
180      *
181      * @param field
182      * @return
183      */
184     private static boolean hasAnnotation(Field field) {
185         List<Class<? extends Annotation>> fieldClazzList = Arrays.asList(
186                 FieldNotNull.class, FieldValue.class, FieldNotNullOr.class, FieldLength.class, FieldNumber.class);
187         for (Class clazz : fieldClazzList) {
188             if (field.isAnnotationPresent(clazz)) {
189                 return true;
190             }
191         }
192         return false;
193     }
194 
195     /**
196      * 获取字段的get方法值
197      *
198      * @param field
199      * @param bizObject
200      * @return
201      * @throws NoSuchMethodException
202      * @throws IllegalAccessException
203      * @throws InvocationTargetException
204      * @author cheng2839
205      * @date 2018年11月16日
206      */
207     private static Object getValue(Field field, Object bizObject)
208             throws IllegalAccessException, InvocationTargetException {
209         String fieldName = field.getName();
210         fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
211         Class clazz = bizObject.getClass();
212 
213         Method method = null;
214         try {
215             method = clazz.getMethod("get" + fieldName);
216         } catch (NoSuchMethodException e) {
217             try {
218                 //如果为boolean型,则get方法为is开头
219                 method = clazz.getMethod("is" + fieldName);
220             } catch (NoSuchMethodException se) {
221                 return field.get(bizObject); //获取该字段的值
222             }
223         }
224         return method.invoke(bizObject);
225     }
226 
227     /**
228      * 获取所有字段(不重复)
229      *
230      * @param clazz
231      * @return
232      * @author cheng2839
233      * @date 2018年11月16日
234      */
235     private static Field[] getAllDeclaredFieldsNoRepeat(Class<?> clazz) {
236         List<Field> fieldList = new ArrayList<>();
237         while (clazz != null) {
238             for (Field f : clazz.getDeclaredFields()) {
239                 if (!fieldList.contains(f)) {
240                     fieldList.add(f);
241                 }
242             }
243             clazz = clazz.getSuperclass();
244         }
245         Field[] fields = new Field[fieldList.size()];
246         return fieldList.toArray(fields);
247     }
248 
249     private static boolean notNull(Object val) {
250         return val!=null && !"".equals(val);
251     }
252 
253     private static boolean values(Object val, String[] values) {
254         if (values!=null) {
255             for (String s:values) {
256                 if (s.equals(val.toString())) {
257                     return true;
258                 }
259             }
260         }
261         return false;
262     }
263 
264     private static boolean length(Object val, int minLen, int maxLen) {
265         return val == null || (String.valueOf(val).length() >= minLen && String.valueOf(val).length() <= maxLen);
266     }
267 
268     private static boolean number(Object val, double min, double max) throws Exception {
269         double valDou = Double.MIN_VALUE;
270         try {
271             valDou = Double.parseDouble(String.valueOf(val));
272         } catch (NumberFormatException e) {
273             throw new Exception(CHECK_ERROR);
274         }
275         return val == null || (valDou >= min && valDou <= max);
276     }
277 }

 

5)终于可以测试了。在这里我模拟了一个用户登录的参数校验,包含以下字段:

 

loginAccount 登录账号: 非空,长度3-8位
loginPwd 登录密码: 非空,最大20位
loginType 登录类型:0:普通账户;1:管理员账户
cmsCheckCode 短信验证码6位:取值范围(100000-999999)
phone、email 电话和邮箱其一必填

  1 package com.cheng2839.test;
  2 
  3 import com.cheng2839.annotation.FieldLength;
  4 import com.cheng2839.annotation.FieldNotNull;
  5 import com.cheng2839.annotation.FieldNotNullOr;
  6 import com.cheng2839.annotation.FieldNumber;
  7 import com.cheng2839.annotation.FieldValue;
  8 import com.cheng2839.enums.ErrorEnum;
  9 
 10 /**
 11  * 入参包装类
 12  */
 13 public class LoginUserVo {
 14 
 15     /**
 16      * 登录账号: 非空,长度3-8位
 17      */
 18     @FieldNotNull(msgEnum = ErrorEnum.PARAM_LOGINACCOUNT_NOTNULL)
 19     @FieldLength(minLen = 3, maxLen = 8, msgEnum = ErrorEnum.PARAM_LOGINACCOUNT_LEN)
 20     private String loginAccount;
 21 
 22     /**
 23      * 登录密码: 非空,最大20位
 24      */
 25     @FieldNotNull(msgEnum = ErrorEnum.PARAM_LOGINPWD_NOTNULL)
 26     @FieldLength(maxLen = 20, msgEnum = ErrorEnum.PARAM_LOGINPWD_LEN)
 27     private String loginPwd;
 28 
 29     /**
 30      * 登录类型:0:普通账户;1:管理员账户
 31      */
 32     @FieldNotNull(msgEnum = ErrorEnum.PARAM_LOGINTYPE_NOTNULL)
 33     @FieldValue(values = {"0", "1"}, msgEnum = ErrorEnum.PARAM_LOGINTYPE_VALUE)
 34     private int loginType;
 35 
 36 
 37     /**
 38      * 短信验证码6位:取值范围(100000-999999)
 39      */
 40     @FieldNotNull(msgEnum = ErrorEnum.PARAM_CMSCHECKCODE_NOTNULL)
 41     @FieldNumber(min = 100000, max = 999999, msgEnum = ErrorEnum.PARAM_CMSCHECKCODE_NUMBER)
 42     private int cmsCheckCode;
 43 
 44     /**
 45      * 电话和邮箱其一必填
 46      */
 47     @FieldNotNullOr(msgEnum = ErrorEnum.PARAM_PHONE_EMAIL_OR)
 48     private String phone;
 49 
 50     /**
 51      * 电话和邮箱其一必填
 52      */
 53     @FieldNotNullOr(msgEnum = ErrorEnum.PARAM_PHONE_EMAIL_OR)
 54     private String email;
 55 
 71       /* getter/setter functions and override toString function */
103 }

 

测试主类如下:

 1 package com.cheng2839.test;
 2 
 3 import com.cheng2839.model.ParamCheckResult;
 4 import com.cheng2839.utils.ParamCheckUtils;
 5 
 6 import java.text.DecimalFormat;
 7 import java.text.SimpleDateFormat;
 8 import java.util.Date;
 9 
10 /**
11  * 测试类
12  */
13 public class TestCheckParam {
14 
15     public static void main(String[] args) throws Exception {
16         LoginUserVo loginUserVo = new LoginUserVo();
17         checkAndPrint(loginUserVo); //第1次
18 
19         loginUserVo.setLoginAccount("cheng2839");
20         checkAndPrint(loginUserVo); //第2次
21 
22         loginUserVo.setLoginAccount("cheng39");
23         loginUserVo.setLoginPwd("abc5789aa");
24         checkAndPrint(loginUserVo); //第3次
25 
26         loginUserVo.setLoginType(2);
27         checkAndPrint(loginUserVo); //第4次
28 
29         loginUserVo.setLoginType(1);
30         loginUserVo.setCmsCheckCode(555);
31         checkAndPrint(loginUserVo); //第5次
32 
33         loginUserVo.setCmsCheckCode(235325);
34         loginUserVo.setPhone(""); //"13392765534"
35         loginUserVo.setEmail(""); //"myemail@126.com"
36         checkAndPrint(loginUserVo); //第6次
37 
38 
39         loginUserVo.setEmail("myemail@126.com"); //"myemail@126.com"
40         checkAndPrint(loginUserVo); //第7次
41     }
42 
43     private static void checkAndPrint(LoginUserVo loginUserVo) throws Exception{
44         println("校验对象:" + loginUserVo);
45         println("--------------------------------------------------------");
46         ParamCheckResult checkResult = ParamCheckUtils.annotationCheck(loginUserVo);
47         if (checkResult.isNotPass()) {
48             println("校验结果:[message:"+ checkResult.getMsgEnum()+"\tcode:"+checkResult.getMsgCode()+"\t ("+checkResult.getFieldName()+":"+checkResult.getValue()+") ]");
49         } else {
50             println("校验结果:[校验通过!]");
51         }
52         println("--------------------------------------------------------");
53     }
54 
55     private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.");
56     private static DecimalFormat decimalFormat = new DecimalFormat("000");
57     private static void println(String messsage) {
58         Date date = new Date();
59         String time = sdf.format(date) + decimalFormat.format(date.getTime() % 1000L);
60         System.out.println(time + "\t[INFO]\t" + messsage);
61     }
62 
63 
64 }

 

测试结果打印日志如下:

 1 xxxx-xx-xx 18:43:59.633    [INFO]    校验对象:LoginUserVo{loginAccount='null', loginPwd='null', loginType=0, cmsCheckCode=0, phone='null', email='null'}
 2 xxxx-xx-xx 18:43:59.634    [INFO]    --------------------------------------------------------
 3 xxxx-xx-xx 18:43:59.648    [INFO]    校验结果:[message:登录账户不能为空!    code:3001     (loginAccount:null) ]
 4 xxxx-xx-xx 18:43:59.649    [INFO]    --------------------------------------------------------
 5 xxxx-xx-xx 18:43:59.649    [INFO]    校验对象:LoginUserVo{loginAccount='cheng2839', loginPwd='null', loginType=0, cmsCheckCode=0, phone='null', email='null'}
 6 xxxx-xx-xx 18:43:59.649    [INFO]    --------------------------------------------------------
 7 xxxx-xx-xx 18:43:59.649    [INFO]    校验结果:[message:登录账户只能为3至8位!    code:3002     (loginAccount:cheng2839) ]
 8 xxxx-xx-xx 18:43:59.649    [INFO]    --------------------------------------------------------
 9 xxxx-xx-xx 18:43:59.649    [INFO]    校验对象:LoginUserVo{loginAccount='cheng39', loginPwd='abc5789aa', loginType=0, cmsCheckCode=0, phone='null', email='null'}
10 xxxx-xx-xx 18:43:59.649    [INFO]    --------------------------------------------------------
11 xxxx-xx-xx 18:43:59.655    [INFO]    校验结果:[message:短信验证码为6位!    code:3008     (cmsCheckCode:0) ]
12 xxxx-xx-xx 18:43:59.655    [INFO]    --------------------------------------------------------
13 xxxx-xx-xx 18:43:59.655    [INFO]    校验对象:LoginUserVo{loginAccount='cheng39', loginPwd='abc5789aa', loginType=2, cmsCheckCode=0, phone='null', email='null'}
14 xxxx-xx-xx 18:43:59.655    [INFO]    --------------------------------------------------------
15 xxxx-xx-xx 18:43:59.655    [INFO]    校验结果:[message:登录类型取值不正确!    code:3006     (loginType:2) ]
16 xxxx-xx-xx 18:43:59.655    [INFO]    --------------------------------------------------------
17 xxxx-xx-xx 18:43:59.656    [INFO]    校验对象:LoginUserVo{loginAccount='cheng39', loginPwd='abc5789aa', loginType=1, cmsCheckCode=555, phone='null', email='null'}
18 xxxx-xx-xx 18:43:59.656    [INFO]    --------------------------------------------------------
19 xxxx-xx-xx 18:43:59.656    [INFO]    校验结果:[message:短信验证码为6位!    code:3008     (cmsCheckCode:555) ]
20 xxxx-xx-xx 18:43:59.656    [INFO]    --------------------------------------------------------
21 xxxx-xx-xx 18:43:59.656    [INFO]    校验对象:LoginUserVo{loginAccount='cheng39', loginPwd='abc5789aa', loginType=1, cmsCheckCode=235325, phone='', email=''}
22 xxxx-xx-xx 18:43:59.656    [INFO]    --------------------------------------------------------
23 xxxx-xx-xx 18:43:59.658    [INFO]    校验结果:[message:电话和邮箱其一必填!    code:3009     (null:null) ]
24 xxxx-xx-xx 18:43:59.658    [INFO]    --------------------------------------------------------
25 xxxx-xx-xx 18:43:59.658    [INFO]    校验对象:LoginUserVo{loginAccount='cheng39', loginPwd='abc5789aa', loginType=1, cmsCheckCode=235325, phone='', email='myemail@126.com'}
26 xxxx-xx-xx 18:43:59.658    [INFO]    --------------------------------------------------------
27 xxxx-xx-xx 18:43:59.658    [INFO]    校验结果:[校验通过!]
28 xxxx-xx-xx 18:43:59.658    [INFO]    --------------------------------------------------------

 

 希望对你有帮助,付出再多努力也值得,谢谢你的阅读和学习。

  本文属于原创,若转载,请注明出处!

 

 

我还是忍不住放上了这张图片,万一你想和我说说话呢😄❀

 

 

posted @ 2020-03-30 17:35  温柔的星空,让你感动  阅读(516)  评论(0编辑  收藏  举报