添加了只有@RequestBody修饰的参数才能校验
This commit is contained in:
parent
bcc29e2cba
commit
6988b8e646
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
@ -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<Field> 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<Field> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user