|
@@ -10,6 +10,8 @@ import javax.validation.Validation;
|
|
|
import javax.validation.Validator;
|
|
|
import javax.validation.ValidatorFactory;
|
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
|
/**
|
|
@@ -19,72 +21,469 @@ import com.alibaba.fastjson.JSON;
|
|
|
* @describe 针对该项目中其它各类数据校验的工具类
|
|
|
*/
|
|
|
public class ValidationUtil {
|
|
|
-
|
|
|
- private ValidationUtil(){
|
|
|
+
|
|
|
+ private ValidationUtil() {
|
|
|
throw new AssertionError("不能实例化 ValidationUtil");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * @Title: validate
|
|
|
- * @Description: TODO(仅针对校验一个实体类的所有校验失败信息)
|
|
|
- * @return List<String> 返回的是数据校验的所有校验失败信息,封装在一个list中作为返回值
|
|
|
- * @author ylx
|
|
|
- * @date 2017年12月26日 下午3:48:40
|
|
|
+ * @Title: validate
|
|
|
+ * @Description: TODO(仅针对校验一个实体类的所有校验失败信息)
|
|
|
+ * @return List<String> 返回的是数据校验的所有校验失败信息,封装在一个list中作为返回值
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static <T> List<String> validate(T bo){
|
|
|
+ public static <T> List<String> validate(T bo) {
|
|
|
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
|
|
Validator validator = factory.getValidator();
|
|
|
Set<ConstraintViolation<T>> validateSet = validator.validate(bo);
|
|
|
- List<String> list=new ArrayList<String>();
|
|
|
+ List<String> list = new ArrayList<String>();
|
|
|
for (ConstraintViolation<T> validate : validateSet) {
|
|
|
list.add(validate.getMessage());
|
|
|
}
|
|
|
- return list; //包含所有校验出错的信息
|
|
|
-
|
|
|
- /*使用hibernate validator出现上面的错误, 需要 注意
|
|
|
- @NotNull 和 @NotEmpty 和@NotBlank 区别
|
|
|
- @NotEmpty 用在集合类上面
|
|
|
- @NotBlank 用在String上面
|
|
|
- @NotNull 用在基本类型上*/
|
|
|
+ return list; // 包含所有校验出错的信息
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 使用hibernate validator出现上面的错误, 需要 注意
|
|
|
+ *
|
|
|
+ * @NotNull 和 @NotEmpty 和@NotBlank 区别
|
|
|
+ *
|
|
|
+ * @NotEmpty 用在集合类上面
|
|
|
+ *
|
|
|
+ * @NotBlank 用在String上面
|
|
|
+ *
|
|
|
+ * @NotNull 用在基本类型上
|
|
|
+ */
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title: range
|
|
|
+ * @Description: TODO(针对传入的参数进行校验范围------>Int类型)
|
|
|
+ * @return boolean
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
+ */
|
|
|
+ public static boolean checkRangeOfInt(final Object obj, int min, int max) {
|
|
|
+ String number = null;
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ throw new NullPointerException();
|
|
|
+ }
|
|
|
+ number = String.class.cast(obj);
|
|
|
+ try {
|
|
|
+ Integer.parseInt(number);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new NumberFormatException();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof NullPointerException) {
|
|
|
+ throw new NullPointerException("对象为null");
|
|
|
+ }
|
|
|
+ if (e instanceof NumberFormatException) {
|
|
|
+ throw new RuntimeException("数据类型解析异常");
|
|
|
+ }
|
|
|
+ Integer num = null;
|
|
|
+ try {
|
|
|
+ num = Integer.class.cast(obj);
|
|
|
+ } catch (Exception e1) {
|
|
|
+ throw new RuntimeException("数字解析异常");
|
|
|
+ }
|
|
|
+ if (num < min) {
|
|
|
+ throw new RuntimeException("最小值应该大于等于"+min);
|
|
|
+ }
|
|
|
+ if(num > max){
|
|
|
+ throw new RuntimeException("最大值应该小于等于"+max);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean checkMinOfInt(final Object obj, int min) {
|
|
|
+ return checkRangeOfInt(obj, min, Integer.MAX_VALUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean checkMaxOfInt(final Object obj, int max) {
|
|
|
+ return checkRangeOfInt(obj, Integer.MIN_VALUE, max);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean checkOfInt(final Object obj) {
|
|
|
+ return checkRangeOfInt(obj, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
- * @Title: range
|
|
|
- * @Description: TODO(针对传入的参数进行校验)
|
|
|
- * @return void
|
|
|
- * @author ylx
|
|
|
- * @date 2017年12月26日 下午3:48:40
|
|
|
+ * @Title: range
|
|
|
+ * @Description: TODO(针对传入的参数进行校验--->Long类型)
|
|
|
+ * @return void
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static void range(Object obj,int min,Class<? extends Number> clazz){
|
|
|
- String number=null;
|
|
|
+ public static boolean checkRangeOfLong(final Object obj, long min, long max) {
|
|
|
+ String number = null;
|
|
|
try {
|
|
|
- number=String.class.cast(obj);
|
|
|
+ if (obj == null) {
|
|
|
+ throw new NullPointerException();
|
|
|
+ }
|
|
|
+ number = String.class.cast(obj);
|
|
|
try {
|
|
|
Long.parseLong(number);
|
|
|
} catch (NumberFormatException e) {
|
|
|
+ throw new NumberFormatException();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof NullPointerException) {
|
|
|
+ throw new NullPointerException("对象为null");
|
|
|
+ }
|
|
|
+ if (e instanceof NumberFormatException) {
|
|
|
+ throw new RuntimeException("数据类型解析异常");
|
|
|
+ }
|
|
|
+ Long num = null;
|
|
|
+ try {
|
|
|
+ num = Long.class.cast(obj);
|
|
|
+ } catch (Exception e1) {
|
|
|
+ throw new RuntimeException("数字解析异常");
|
|
|
+ }
|
|
|
+ if (num < min) {
|
|
|
+ throw new RuntimeException("最小值应该大于等于"+min);
|
|
|
+ }
|
|
|
+ if(num > max){
|
|
|
+ throw new RuntimeException("最大值应该小于等于"+max);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean checkMinOfLong(final Object obj, long min) {
|
|
|
+ return checkRangeOfLong(obj, min, Long.MAX_VALUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean checkMaxOfLong(final Object obj, long max) {
|
|
|
+ return checkRangeOfLong(obj, Long.MIN_VALUE, max);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean checkOfLong(final Object obj, long max) {
|
|
|
+ return checkRangeOfLong(obj, Long.MIN_VALUE, Long.MAX_VALUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title: range
|
|
|
+ * @Description: TODO(针对传入的参数进行校验--->Int类型,校验和赋值同时进行,同时返回真实的int数值)
|
|
|
+ * @return int
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
+ */
|
|
|
+ public static int checkRangeAndAssignInt(final Object obj, int min, int max) {
|
|
|
+ String number = null;
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ throw new NullPointerException();
|
|
|
+ }
|
|
|
+ number = String.class.cast(obj);
|
|
|
+ try {
|
|
|
+ return Integer.parseInt(number);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new NumberFormatException();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof NullPointerException) {
|
|
|
+ throw new NullPointerException("对象为null");
|
|
|
+ }
|
|
|
+ if (e instanceof NumberFormatException) {
|
|
|
throw new RuntimeException("数据类型解析异常");
|
|
|
}
|
|
|
+ Integer num = null;
|
|
|
+ try {
|
|
|
+ num = Integer.class.cast(obj);
|
|
|
+ } catch (Exception e1) {
|
|
|
+ throw new RuntimeException("数字解析异常");
|
|
|
+ }
|
|
|
+ if (num < min) {
|
|
|
+ throw new RuntimeException("最小值应该大于等于"+min);
|
|
|
+ }
|
|
|
+ if(num > max){
|
|
|
+ throw new RuntimeException("最大值应该小于等于"+max);
|
|
|
+ }
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int checkMinAndAssignInt(final Object obj, int min) {
|
|
|
+ return checkRangeAndAssignInt(obj,min,Integer.MAX_VALUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int checkMaxAndAssignInt(final Object obj, int max) {
|
|
|
+ return checkRangeAndAssignInt(obj,Integer.MIN_VALUE,max);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int checkAndAssignInt(final Object obj) {
|
|
|
+ return checkRangeAndAssignInt(obj,Integer.MIN_VALUE,Integer.MAX_VALUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title: range
|
|
|
+ * @Description: TODO(针对传入的参数进行校验--->Long类型,校验和赋值同时进行,同时返回真实的long数值)
|
|
|
+ * @return long
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
+ */
|
|
|
+ public static long checkRangeAndAssignLong(final Object obj, long min, long max) {
|
|
|
+ String number = null;
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ throw new NullPointerException();
|
|
|
+ }
|
|
|
+ number = String.class.cast(obj);
|
|
|
+ try {
|
|
|
+ return Long.parseLong(number);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new NumberFormatException();
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- if(e.getMessage().equals("数据类型解析异常")){
|
|
|
+ if (e instanceof NullPointerException) {
|
|
|
+ throw new NullPointerException("对象为null");
|
|
|
+ }
|
|
|
+ if (e instanceof NumberFormatException) {
|
|
|
throw new RuntimeException("数据类型解析异常");
|
|
|
}
|
|
|
- Number num=null;
|
|
|
+ Long num = null;
|
|
|
try {
|
|
|
- num = clazz.cast(obj);
|
|
|
+ num = Long.class.cast(obj);
|
|
|
} catch (Exception e1) {
|
|
|
throw new RuntimeException("数字解析异常");
|
|
|
}
|
|
|
- if(num.longValue()<min){
|
|
|
- throw new RuntimeException("最小值应为"+min);
|
|
|
+ if (num < min) {
|
|
|
+ throw new RuntimeException("最小值应该大于等于"+min);
|
|
|
+ }
|
|
|
+ if(num > max){
|
|
|
+ throw new RuntimeException("最大值应该小于等于"+max);
|
|
|
}
|
|
|
+ return num;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- Map<String,Object> map = JSON.parseObject("{'KEY1':'033','KEY2':5}", Map.class);
|
|
|
- range(map.get("KEY1"), 0, Integer.class);
|
|
|
+ public static long checkMinAndAssignLong(final Object obj, long min) {
|
|
|
+ return checkRangeAndAssignLong(obj,min,Long.MAX_VALUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static long checkMaxAndAssignLong(final Object obj, long max) {
|
|
|
+ return checkRangeAndAssignLong(obj,Long.MIN_VALUE,max);
|
|
|
}
|
|
|
|
|
|
+ public static long checkAndAssignLong(final Object obj) {
|
|
|
+ return checkRangeAndAssignLong(obj,Long.MIN_VALUE,Long.MAX_VALUE);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @Title: range
|
|
|
+ * @Description: TODO(针对传入的参数进行非空(null)校验并返回string数据类型--->只针对String类型,校验和赋值同时进行,同时返回真实的String值)
|
|
|
+ * @return long
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
+ */
|
|
|
+ public static String checkNullAndAssignString(final Object obj){
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ throw new NullPointerException();
|
|
|
+ }
|
|
|
+ String str=String.class.cast(obj);
|
|
|
+ return str;
|
|
|
+ } catch (Exception e) {
|
|
|
+ if(e instanceof NullPointerException){
|
|
|
+ throw new NullPointerException("对象为null");
|
|
|
+ }
|
|
|
+ if(e instanceof ClassCastException){
|
|
|
+ throw new ClassCastException("强转String类型异常");
|
|
|
+ }
|
|
|
+ throw new RuntimeException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title: range
|
|
|
+ * @Description: TODO(针对传入的参数进行非空(null)和空字符串("")校验并返回string数据类型--->只针对String类型,校验和赋值同时进行,同时返回真实的String值)
|
|
|
+ * @return long
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
+ */
|
|
|
+ public static String checkEmptyAndAssignString(final Object obj){
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ throw new NullPointerException();
|
|
|
+ }
|
|
|
+ String str=String.class.cast(obj);
|
|
|
+ if("".equals(str)){
|
|
|
+ throw new RuntimeException("字符串不能为空");
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ } catch (Exception e) {
|
|
|
+ if(e instanceof NullPointerException){
|
|
|
+ throw new NullPointerException("对象为null");
|
|
|
+ }
|
|
|
+ if(e instanceof ClassCastException){
|
|
|
+ throw new ClassCastException("强转String类型异常");
|
|
|
+ }
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title: range
|
|
|
+ * @Description: TODO(针对传入的参数进行非空(null)和空字符串("")和空内容的字符串(" ")校验并返回string数据类型--->只针对String类型,校验和赋值同时进行,同时返回真实的String值)
|
|
|
+ * @return long
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
+ */
|
|
|
+ public static String checkBlankAndAssignString(final Object obj){
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ throw new NullPointerException();
|
|
|
+ }
|
|
|
+ String str=String.class.cast(obj);
|
|
|
+ if("".equals(str.trim())){
|
|
|
+ throw new RuntimeException("字符串不能无任何内容");
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ } catch (Exception e) {
|
|
|
+ if(e instanceof NullPointerException){
|
|
|
+ throw new NullPointerException("对象为null");
|
|
|
+ }
|
|
|
+ if(e instanceof ClassCastException){
|
|
|
+ throw new ClassCastException("强转String类型异常");
|
|
|
+ }
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title: range
|
|
|
+ * @Description: TODO(针对传入的参数进行非空(null)和空字符串("")和空内容的字符串(" ")校验并返回string数据类型,只要是null,""或" "全部返回null,不会抛出异常,除非无法强转为String,其它情况正常转String)
|
|
|
+ * @return long
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
+ */
|
|
|
+ public static String checkBlankStringAndAssignNullIfIsBlank(final Object obj){
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String str=String.class.cast(obj);
|
|
|
+ if("".equals(str.trim())){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("String类型转换异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title: range
|
|
|
+ * @Description: TODO(针对传入的参数进行非空(null)和空字符串("")和空内容的字符串(" ")校验并返回string数据类型,只要是null,""或" "全部返回""空串,不会抛出异常,除非无法强转为String,其它情况正常转String)
|
|
|
+ * @return long
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
+ */
|
|
|
+ public static String checkBlankStringAndAssignEmptyIfIsBlank(final Object obj){
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String str=String.class.cast(obj);
|
|
|
+ if("".equals(str.trim())){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("String类型转换异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @Title: checkAndAssignDefaultInt
|
|
|
+ * @Description: TODO(如果校验的Object不能转换Int则抛出异常,如果是null,空字符串或者是长空串,统一返回默认的值)
|
|
|
+ * @return int 返回类型
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月27日 下午1:51:43
|
|
|
+ */
|
|
|
+ public static int checkAndAssignDefaultInt(final Object obj,int defaultInt){
|
|
|
+ String number = null;
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ return defaultInt;
|
|
|
+ }
|
|
|
+ number = String.class.cast(obj);
|
|
|
+ if("".equals(number.trim())){
|
|
|
+ return defaultInt;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Integer.parseInt(number);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new NumberFormatException();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof NumberFormatException) {
|
|
|
+ throw new RuntimeException("数据类型解析异常");
|
|
|
+ }
|
|
|
+ Integer num = null;
|
|
|
+ try {
|
|
|
+ num = Integer.class.cast(obj);
|
|
|
+ } catch (Exception e1) {
|
|
|
+ throw new RuntimeException("数字解析异常");
|
|
|
+ }
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @Title: checkAndAssignDefaultLong
|
|
|
+ * @Description: TODO(如果校验的Object不能转换Long则抛出异常,如果是null,空字符串或者是长空串,统一返回默认的值)
|
|
|
+ * @Attention: 这里要特别注意以下,如果前端传过来的数据是小于Integer.MAX_VALUE的话,必须要加L来将数据强制转化为Long类型,不然解析失败
|
|
|
+ * @return long 返回类型
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月27日 下午1:51:43
|
|
|
+ */
|
|
|
+ public static long checkAndAssignDefaultLong(final Object obj,long defaultLong){
|
|
|
+ String number = null;
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ return defaultLong;
|
|
|
+ }
|
|
|
+ number = String.class.cast(obj);
|
|
|
+ if("".equals(number.trim())){
|
|
|
+ return defaultLong;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Long.parseLong(number);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new NumberFormatException();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof NumberFormatException) {
|
|
|
+ throw new RuntimeException("数据类型解析异常");
|
|
|
+ }
|
|
|
+ Long num = null;
|
|
|
+ try {
|
|
|
+ num=Long.parseLong(""+obj);
|
|
|
+ } catch (Exception e1) {
|
|
|
+ throw new RuntimeException("数字解析异常");
|
|
|
+ }
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String json="{'KEY1':'12','KEY2':23,'KEY3':null}";
|
|
|
+ Map parseObject = JSON.parseObject(json, Map.class);
|
|
|
+ Object obj=20000000;
|
|
|
+ long checkAndAssignDefaultLong = checkAndAssignDefaultLong(obj,1000);
|
|
|
+ System.out.println(checkAndAssignDefaultLong);
|
|
|
+// Long.class.cast(obj
|
|
|
+// Long cast = Long.class.cast(1L);
|
|
|
+// System.out.println(checkAndAssignDefaultLong);
|
|
|
+ }
|
|
|
+
|
|
|
}
|