diff --git a/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiParamsAspect.java b/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiParamsAspect.java index 91d6be5..8029a02 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiParamsAspect.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiParamsAspect.java @@ -2,13 +2,10 @@ package com.cm.common.aspect; import com.cm.common.annotation.CheckRequestBodyAnnotation; import com.cm.common.exception.ParamsException; -import com.cm.common.exception.base.SystemException; import com.cm.common.utils.annotation.AnnotationUtil; -import com.google.inject.internal.cglib.core.$ClassInfo; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; -import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @@ -16,10 +13,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestBody; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; +import java.lang.reflect.Parameter; /** * When you feel like quitting. Think about why you started @@ -72,10 +69,18 @@ public class ApiParamsAspect { if (method == null) { return; } + if (method.isAnnotationPresent(CheckRequestBodyAnnotation.class)) { LOG.debug("校验参数"); - for (Object arg : args) { - AnnotationUtil.checkField(arg); + Parameter[] parameters = method.getParameters(); + for(Parameter parameter : parameters) { + if(parameter.isAnnotationPresent(RequestBody.class)) { + for(Object arg: args) { + if(parameter.getType() == arg.getClass()) { + AnnotationUtil.checkField(arg); + } + } + } } } } diff --git a/cloud-common/src/main/java/com/cm/common/annotation/CheckNumberAnnotation.java b/cloud-common/src/main/java/com/cm/common/annotation/CheckNumberAnnotation.java index 167eb9a..cde1040 100644 --- a/cloud-common/src/main/java/com/cm/common/annotation/CheckNumberAnnotation.java +++ b/cloud-common/src/main/java/com/cm/common/annotation/CheckNumberAnnotation.java @@ -23,8 +23,8 @@ public @interface CheckNumberAnnotation { String[] types() default {}; - double max() default Double.MIN_VALUE; + double max() default Double.MAX_VALUE; - double min() default Double.MAX_VALUE; + double min() default Double.MIN_VALUE; } diff --git a/cloud-common/src/main/java/com/cm/common/utils/annotation/AnnotationUtil.java b/cloud-common/src/main/java/com/cm/common/utils/annotation/AnnotationUtil.java index 0724f97..b1cbc9f 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/annotation/AnnotationUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/annotation/AnnotationUtil.java @@ -8,6 +8,8 @@ import org.apache.commons.lang3.math.NumberUtils; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -30,7 +32,8 @@ public class AnnotationUtil { */ public static void checkField(Object object) throws ParamsException { Class clazz = object.getClass(); - Field[] fields = clazz.getDeclaredFields(); + List fields = new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())); + setSuperClassField(clazz, fields); Method[] methods = clazz.getMethods(); try { for (Field field : fields) { @@ -135,4 +138,23 @@ public class AnnotationUtil { return null; } + /** + * 设置父类属性列表 + * + * @param clazz + * @param fields + */ + private static void setSuperClassField(Class clazz, List fields) { + Class superClazz = clazz.getSuperclass(); + if (superClazz == null) { + return; + } + Field[] superFields = superClazz.getDeclaredFields(); + if (superFields == null) { + return; + } + fields.addAll(Arrays.asList(superFields)); + setSuperClassField(superClazz, fields); + } + }