新增列表校验注解,list,map转bean,dateUtil放出正则
This commit is contained in:
parent
47ffe22e1d
commit
337b2a7ebb
@ -0,0 +1,22 @@
|
||||
package com.cm.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: CheckListAnnotation
|
||||
* @Description: 校验List
|
||||
* @Author: WangGeng
|
||||
* @Date: 2019/11/15 14:39
|
||||
* @Version: 1.0
|
||||
**/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
public @interface CheckListAnnotation {
|
||||
|
||||
String name();
|
||||
|
||||
}
|
@ -23,8 +23,8 @@ public @interface CheckNumberAnnotation {
|
||||
|
||||
String[] types() default {};
|
||||
|
||||
double max() default 0;
|
||||
double max() default Double.MIN_VALUE;
|
||||
|
||||
double min() default 0;
|
||||
double min() default Double.MAX_VALUE;
|
||||
|
||||
}
|
||||
|
@ -16,15 +16,15 @@ public class DateUtil {
|
||||
|
||||
public final static String AM = "AM";
|
||||
public final static String PM = "PM";
|
||||
private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
|
||||
private final static SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
|
||||
private final static SimpleDateFormat sdfd = new SimpleDateFormat("dd");
|
||||
private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
|
||||
private final static SimpleDateFormat sdfDayZh = new SimpleDateFormat("yyyy年MM月dd日");
|
||||
private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
|
||||
private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
private final static SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
private final static SimpleDateFormat sdfYearMonth = new SimpleDateFormat("yyyyMM");
|
||||
public final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
|
||||
public final static SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
|
||||
public final static SimpleDateFormat sdfd = new SimpleDateFormat("dd");
|
||||
public final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
|
||||
public final static SimpleDateFormat sdfDayZh = new SimpleDateFormat("yyyy年MM月dd日");
|
||||
public final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
|
||||
public final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
public final static SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
public final static SimpleDateFormat sdfYearMonth = new SimpleDateFormat("yyyyMM");
|
||||
|
||||
public static String getZhDate(String date) {
|
||||
try {
|
||||
|
@ -2,6 +2,8 @@ package com.cm.common.utils;
|
||||
|
||||
import com.cm.common.pojo.vos.push.PushMessageVO;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.beans.BeanInfo;
|
||||
@ -10,9 +12,9 @@ import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
@ -179,20 +181,60 @@ public class HashMapUtil {
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
Object instanceObj = clazz.newInstance();
|
||||
for (Map.Entry<String, Object> kvs : map.entrySet()) {
|
||||
Object value = kvs.getValue();
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
String fieldName = field.getName();
|
||||
if (StringUtils.equals(fieldName, kvs.getKey())) {
|
||||
Method method = clazz.getMethod("set" + WStringUtil.firstToUpper(fieldName), field.getType());
|
||||
method.invoke(instanceObj, kvs.getValue());
|
||||
if (field.getType() == int.class) {
|
||||
method.invoke(instanceObj, Integer.parseInt(value.toString()));
|
||||
} else if (field.getType() == String.class) {
|
||||
method.invoke(instanceObj, value.toString());
|
||||
} else if (field.getType() == Date.class) {
|
||||
method.invoke(instanceObj, DateUtil.sdfTime.parse(value.toString()));
|
||||
} else if (field.getType() == Double.class) {
|
||||
method.invoke(instanceObj, Double.parseDouble(value.toString()));
|
||||
} else if (field.getType() == Float.class) {
|
||||
method.invoke(instanceObj, Float.parseFloat(value.toString()));
|
||||
} else if (field.getType() == Long.class) {
|
||||
method.invoke(instanceObj, Long.parseLong(value.toString()));
|
||||
} else if (field.getType() == Boolean.class) {
|
||||
method.invoke(instanceObj, Boolean.parseBoolean(value.toString()));
|
||||
} else if (field.getType() == Short.class) {
|
||||
method.invoke(instanceObj, Short.parseShort(value.toString()));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return clazz.cast(instanceObj);
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* map队列转bean队列
|
||||
*
|
||||
* @param listMap
|
||||
* @param clazz
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> List<T> mapListToBeanList(List<Map<String, Object>> listMap, Class<T> clazz) {
|
||||
List<T> list = new ArrayList<>();
|
||||
if (listMap == null || listMap.isEmpty()) {
|
||||
return list;
|
||||
}
|
||||
for (Map<String, Object> map : listMap) {
|
||||
list.add(mapToBean(map, clazz));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,14 @@
|
||||
package com.cm.common.utils.annotation;
|
||||
|
||||
import com.cm.common.annotation.CheckBooleanAnnotation;
|
||||
import com.cm.common.annotation.CheckEmptyAnnotation;
|
||||
import com.cm.common.annotation.CheckNullAnnotation;
|
||||
import com.cm.common.annotation.CheckNumberAnnotation;
|
||||
import com.cm.common.annotation.*;
|
||||
import com.cm.common.exception.ParamsException;
|
||||
import com.cm.common.exception.base.SystemException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
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.List;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
@ -48,27 +45,47 @@ public class AnnotationUtil {
|
||||
throw new ParamsException(String.format("%s不能为空", checkNullAnnotation.name()));
|
||||
}
|
||||
checkTypes(checkNullAnnotation.name(), fieldValue.toString(), checkNullAnnotation.types());
|
||||
}
|
||||
if (field.isAnnotationPresent(CheckEmptyAnnotation.class)) {
|
||||
} else if (field.isAnnotationPresent(CheckEmptyAnnotation.class)) {
|
||||
CheckEmptyAnnotation checkEmptyAnnotation = field.getAnnotation(CheckEmptyAnnotation.class);
|
||||
if (StringUtils.isBlank(fieldValue.toString())) {
|
||||
throw new ParamsException(String.format("%s不能为空或空串", checkEmptyAnnotation.name()));
|
||||
}
|
||||
checkTypes(checkEmptyAnnotation.name(), fieldValue.toString(), checkEmptyAnnotation.types());
|
||||
}
|
||||
if (field.isAnnotationPresent(CheckNumberAnnotation.class)) {
|
||||
} else if (field.isAnnotationPresent(CheckNumberAnnotation.class)) {
|
||||
CheckNumberAnnotation checkNumberAnnotation = field.getAnnotation(CheckNumberAnnotation.class);
|
||||
if (fieldValue == null || !NumberUtils.isNumber(fieldValue.toString())) {
|
||||
throw new ParamsException(String.format("%s必须为数字", checkNumberAnnotation.name()));
|
||||
}
|
||||
checkTypes(checkNumberAnnotation.name(), fieldValue.toString(), checkNumberAnnotation.types());
|
||||
Double value = Double.parseDouble(fieldValue.toString());
|
||||
if (value < checkNumberAnnotation.min()) {
|
||||
throw new ParamsException(String.format("%s最小值为%f", checkNumberAnnotation.name(), checkNumberAnnotation.min()));
|
||||
}
|
||||
if (field.isAnnotationPresent(CheckBooleanAnnotation.class)) {
|
||||
if (value > checkNumberAnnotation.max()) {
|
||||
throw new ParamsException(String.format("%s最大值为%f", checkNumberAnnotation.name(), checkNumberAnnotation.max()));
|
||||
}
|
||||
checkTypes(checkNumberAnnotation.name(), fieldValue.toString(), checkNumberAnnotation.types());
|
||||
} else if (field.isAnnotationPresent(CheckBooleanAnnotation.class)) {
|
||||
CheckBooleanAnnotation checkBooleanAnnotation = field.getAnnotation(CheckBooleanAnnotation.class);
|
||||
if (fieldValue == null) {
|
||||
throw new ParamsException(String.format("%s必须为布尔", checkBooleanAnnotation.name()));
|
||||
}
|
||||
checkTypes(checkBooleanAnnotation.name(), fieldValue.toString(), checkBooleanAnnotation.types());
|
||||
} else if (field.isAnnotationPresent(CheckListAnnotation.class)) {
|
||||
CheckListAnnotation checkListAnnotation = field.getAnnotation(CheckListAnnotation.class);
|
||||
if (fieldValue == null) {
|
||||
throw new ParamsException(String.format("%s不能为空", checkListAnnotation.name()));
|
||||
}
|
||||
if (fieldValue instanceof List) {
|
||||
List fieldValueList = (List) fieldValue;
|
||||
for (Object obj : fieldValueList) {
|
||||
checkField(obj);
|
||||
}
|
||||
} else if (fieldValue instanceof String[]) {
|
||||
String[] fieldValueArray = (String[]) fieldValue;
|
||||
for (Object obj : fieldValueArray) {
|
||||
checkField(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
|
Loading…
Reference in New Issue
Block a user