java-example:validationutil
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java-example:validationutil [2021/12/20 09:29] – morgan0329 | java-example:validationutil [2021/12/20 09:33] (current) – [ValidationF] morgan0329 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== BeanUtil ====== | ||
+ | <code java> | ||
+ | public static <T> BaseResp< | ||
+ | BaseResp< | ||
+ | try { | ||
+ | baseVO = new ValidationUtil<> | ||
+ | } catch (IllegalAccessException ex) { | ||
+ | throw new InternalError(" | ||
+ | } | ||
+ | return baseVO; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== ServiceConfig ====== | ||
+ | <code java> | ||
+ | // | ||
+ | @Around(" | ||
+ | public Object doAround(ProceedingJoinPoint joinPoint) { | ||
+ | Object result; | ||
+ | try { | ||
+ | StringBuilder sb = new StringBuilder(); | ||
+ | |||
+ | Signature signature = joinPoint.getSignature(); | ||
+ | if (signature != null) { | ||
+ | sb.append(signature.getDeclaringType() + " method=>" | ||
+ | } | ||
+ | |||
+ | Object[] args = joinPoint.getArgs(); | ||
+ | // | ||
+ | for (Object arg : args) { | ||
+ | BaseResp resp = BeanUtil.validate(arg); | ||
+ | if (resp != null) { | ||
+ | logger.info(LOG_SERVICE_RESP, | ||
+ | return resp; | ||
+ | } | ||
+ | } | ||
+ | // | ||
+ | result = joinPoint.proceed(); | ||
+ | |||
+ | } catch (Throwable t) { | ||
+ | } | ||
+ | |||
+ | return result; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== ValidationF ====== | ||
+ | <code java> | ||
+ | package com.xiaosq.common | ||
+ | import java.lang.annotation.ElementType; | ||
+ | import java.lang.annotation.Retention; | ||
+ | import java.lang.annotation.RetentionPolicy; | ||
+ | import java.lang.annotation.Target; | ||
+ | |||
+ | /** | ||
+ | * @author Morgan.L | ||
+ | * @version 1.0 | ||
+ | * @date 4/9/2020 4:15 PM | ||
+ | */ | ||
+ | @Retention(RetentionPolicy.RUNTIME) | ||
+ | @Target(ElementType.FIELD) | ||
+ | public @interface ValidationF { | ||
+ | |||
+ | enum Type {PhoneNo, NotNull, BankCardNo, NotBlank, IdCardNo} | ||
+ | |||
+ | Type type(); | ||
+ | |||
+ | String[] msgParam(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== ValidationUtil ====== | ||
+ | <code java> | ||
+ | package com.xiaosq.common; | ||
+ | |||
+ | import com.xiaosq.common.BaseResp; | ||
+ | import com.xiaosq.common.CodeMsgEnum; | ||
+ | import org.apache.commons.lang3.StringUtils; | ||
+ | |||
+ | import java.lang.annotation.Annotation; | ||
+ | import java.lang.reflect.AccessibleObject; | ||
+ | import java.lang.reflect.Field; | ||
+ | |||
+ | /** | ||
+ | * @author Morgan.L | ||
+ | * @version 1.0 | ||
+ | * @date 4/9/2020 4:13 PM | ||
+ | * <p> | ||
+ | * 对于前端web层验证:把所有参数验证完了,再返回验证信息。 | ||
+ | * 对于Service层验证:发现一个错误立刻停止验证,并返回错误信息。 | ||
+ | */ | ||
+ | public class ValidationUtil< | ||
+ | |||
+ | private final T object; | ||
+ | |||
+ | public ValidationUtil(T object) { | ||
+ | this.object = object; | ||
+ | } | ||
+ | |||
+ | public T getObject() { | ||
+ | return object; | ||
+ | } | ||
+ | |||
+ | public BaseResp< | ||
+ | |||
+ | if (this.getObject() == null) { | ||
+ | return null; | ||
+ | } | ||
+ | |||
+ | Class<?> | ||
+ | // 获取实体类的所有属性,返回Field数组 | ||
+ | Field[] fields = clazz.getDeclaredFields(); | ||
+ | AccessibleObject.setAccessible(fields, | ||
+ | for (Field field : fields) { | ||
+ | for (Annotation annotation : field.getDeclaredAnnotations()) { | ||
+ | if (annotation instanceof ValidationF) { | ||
+ | ValidationF validation = (ValidationF) annotation; | ||
+ | |||
+ | if (ValidationF.Type.NotBlank.equals(validation.type())) { | ||
+ | |||
+ | if (field.get(object) == null || StringUtils.isBlank(field.get(object) + "" | ||
+ | return BeanUtil.createResp(CodeMsgEnum.PARAM_IS_BLANK, | ||
+ | } | ||
+ | |||
+ | } else if (ValidationF.Type.BankCardNo.equals(validation.type())) { | ||
+ | |||
+ | if (field.get(object) == null || StringUtils.isBlank((String) field.get(object))) { | ||
+ | return BeanUtil.createResp(CodeMsgEnum.PARAM_IS_BLANK, | ||
+ | } else { | ||
+ | String fieldValue = (String) field.get(object); | ||
+ | if (fieldValue.length() < 16 || fieldValue.length() > 19) { | ||
+ | return BeanUtil.createResp(CodeMsgEnum.INVALID_BANK_CARD_NO, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } else if (ValidationF.Type.PhoneNo.equals(validation.type())) { | ||
+ | if (field.get(object) == null || StringUtils.isBlank((String) field.get(object))) { | ||
+ | return BeanUtil.createResp(CodeMsgEnum.PARAM_IS_BLANK, | ||
+ | } else { | ||
+ | String fieldValue = ((String) field.get(object)); | ||
+ | if (fieldValue.length() != 11 || !fieldValue.startsWith(" | ||
+ | return BeanUtil.createResp(CodeMsgEnum.INVALID_PHONE_NO, | ||
+ | } | ||
+ | } | ||
+ | } else if (ValidationF.Type.IdCardNo.equals(validation.type())) { | ||
+ | if (field.get(object) == null || StringUtils.isBlank((String) field.get(object))) { | ||
+ | return BeanUtil.createResp(CodeMsgEnum.PARAM_IS_BLANK, | ||
+ | } else { | ||
+ | String fieldValue = (String) field.get(object); | ||
+ | if (fieldValue.length() > 18) { | ||
+ | return BeanUtil.createResp(CodeMsgEnum.INVALID_ID_CARD_NO, | ||
+ | } | ||
+ | } | ||
+ | } else if (ValidationF.Type.NotNull.equals(validation.type())) { | ||
+ | if (field.get(object) == null) { | ||
+ | return BeanUtil.createResp(CodeMsgEnum.PARAM_IS_NULL, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | return null; | ||
+ | } | ||
+ | } | ||
+ | </ | ||