|
1 | 1 | package com.github.kuangcp.validation; |
2 | 2 |
|
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | + |
| 5 | +import javax.validation.ConstraintViolation; |
| 6 | +import javax.validation.Validation; |
| 7 | +import javax.validation.Validator; |
| 8 | +import javax.validation.ValidatorFactory; |
3 | 9 | import java.util.Arrays; |
4 | 10 | import java.util.Collection; |
5 | 11 | import java.util.Collections; |
|
8 | 14 | import java.util.Map; |
9 | 15 | import java.util.Objects; |
10 | 16 | import java.util.Set; |
11 | | -import javax.validation.ConstraintViolation; |
12 | | -import javax.validation.Validation; |
13 | | -import javax.validation.Validator; |
14 | | -import javax.validation.ValidatorFactory; |
15 | | -import lombok.extern.slf4j.Slf4j; |
16 | 17 |
|
17 | 18 | /** |
18 | 19 | * 基于注解的校验, @NotBlank @NotEmpty.... |
|
21 | 22 | @Slf4j |
22 | 23 | public abstract class BeanValidator { |
23 | 24 |
|
24 | | - private static final ValidatorFactory validatorFactory = Validation |
25 | | - .buildDefaultValidatorFactory(); |
| 25 | + private static final ValidatorFactory validatorFactory = Validation |
| 26 | + .buildDefaultValidatorFactory(); |
26 | 27 |
|
27 | | - /** |
28 | | - * 校验多个字段 |
29 | | - * |
30 | | - * @param t 校验的对象 |
31 | | - * @param groups Class, 必须要传,如果没有就传入一个new Class[0] |
32 | | - * @param <T> 校验的对象泛型 |
33 | | - * @return key: 字段,value:错误信息 |
34 | | - */ |
35 | | - public static <T> Map<String, String> validate(T t, Class... groups) { |
36 | | - Validator validator = validatorFactory.getValidator(); |
37 | | - Set<ConstraintViolation<T>> violationSet = validator.validate(t, groups); |
38 | | - if (violationSet.isEmpty()) { |
39 | | - return Collections.emptyMap(); |
40 | | - } else { |
41 | | - Map<String, String> errors = new HashMap<>(); |
42 | | - for (ConstraintViolation<T> violation : violationSet) { |
43 | | - errors.put(violation.getPropertyPath().toString(), violation.getMessage()); |
44 | | - } |
45 | | - return errors; |
| 28 | + /** |
| 29 | + * 校验多个字段 |
| 30 | + * |
| 31 | + * @param t 校验的对象 |
| 32 | + * @param groups Class, 必须要传,如果没有就传入一个new Class[0] |
| 33 | + * @param <T> 校验的对象泛型 |
| 34 | + * @return key: 字段,value:错误信息 |
| 35 | + */ |
| 36 | + public static <T> Map<String, String> validate(T t, Class... groups) { |
| 37 | + Validator validator = validatorFactory.getValidator(); |
| 38 | + Set<ConstraintViolation<T>> violationSet = validator.validate(t, groups); |
| 39 | + if (violationSet.isEmpty()) { |
| 40 | + return Collections.emptyMap(); |
| 41 | + } else { |
| 42 | + Map<String, String> errors = new HashMap<>(); |
| 43 | + for (ConstraintViolation<T> violation : violationSet) { |
| 44 | + errors.put(violation.getPropertyPath().toString(), violation.getMessage()); |
| 45 | + } |
| 46 | + return errors; |
| 47 | + } |
46 | 48 | } |
47 | | - } |
48 | 49 |
|
49 | | - /** |
50 | | - * 校验多个对象 |
51 | | - * |
52 | | - * @param collection 集合 |
53 | | - * @return key: 字段,value:错误信息 |
54 | | - */ |
55 | | - public static Map<String, String> validateList(Collection<?> collection) { |
56 | | - Iterator<?> iterator = collection.iterator(); |
57 | | - Map<String, String> errors; |
58 | | - do { |
59 | | - if (!iterator.hasNext()) { |
60 | | - return Collections.emptyMap(); |
61 | | - } |
62 | | - Object object = iterator.next(); |
63 | | - // new Class[0] 必须要传, 否则在determineGroupValidationOrder方法中,会抛出异常 |
64 | | - errors = validate(object, new Class[0]); |
65 | | - } while (errors.isEmpty()); |
66 | | - return errors; |
67 | | - } |
| 50 | + /** |
| 51 | + * 校验多个对象 |
| 52 | + * |
| 53 | + * @param collection 集合 |
| 54 | + * @return key: 字段,value:错误信息 |
| 55 | + */ |
| 56 | + public static Map<String, String> validateList(Collection<?> collection) { |
| 57 | + Iterator<?> iterator = collection.iterator(); |
| 58 | + Map<String, String> errors; |
| 59 | + do { |
| 60 | + if (!iterator.hasNext()) { |
| 61 | + return Collections.emptyMap(); |
| 62 | + } |
| 63 | + Object object = iterator.next(); |
| 64 | + // new Class[0] 必须要传, 否则在determineGroupValidationOrder方法中,会抛出异常 |
| 65 | + errors = validate(object, new Class[0]); |
| 66 | + } while (errors.isEmpty()); |
| 67 | + return errors; |
| 68 | + } |
68 | 69 |
|
69 | | - /** |
70 | | - * 综合validateList 和 validate 方法, 任何校验只需要使用这个方法就可以 |
71 | | - * |
72 | | - * @param first 第一个对象 |
73 | | - * @param objects 对象数组 |
74 | | - * @return key: 字段,value:错误信息 |
75 | | - */ |
76 | | - public static Map<String, String> validateObject(Object first, Object... objects) { |
77 | | - if (objects != null && objects.length > 0) { |
78 | | - return validateList(Arrays.asList(first, objects)); |
79 | | - } else { |
80 | | - // new Class[0] 必须要传, 否则在determineGroupValidationOrder方法中,会抛出异常 |
81 | | - return validate(first, new Class[0]); |
| 70 | + /** |
| 71 | + * 综合validateList 和 validate 方法, 任何校验只需要使用这个方法就可以 |
| 72 | + * |
| 73 | + * @param first 第一个对象 |
| 74 | + * @param objects 对象数组 |
| 75 | + * @return key: 字段,value:错误信息 |
| 76 | + */ |
| 77 | + public static Map<String, String> validateObject(Object first, Object... objects) { |
| 78 | + if (objects != null && objects.length > 0) { |
| 79 | + return validateList(Arrays.asList(first, objects)); |
| 80 | + } else { |
| 81 | + // new Class[0] 必须要传, 否则在determineGroupValidationOrder方法中,会抛出异常 |
| 82 | + return validate(first, new Class[0]); |
| 83 | + } |
82 | 84 | } |
83 | | - } |
84 | 85 |
|
85 | | - /** |
86 | | - * 再次封装校验,对于异常直接抛出 |
87 | | - * |
88 | | - * @param param 参数 |
89 | | - */ |
90 | | - public static void check(Object param) throws IllegalArgumentException { |
91 | | - Map<String, String> validateMap = BeanValidator.validateObject(param); |
92 | | - if (Objects.nonNull(validateMap) && !validateMap.isEmpty()) { |
93 | | - throw new RuntimeException(validateMap.values().iterator().next()); |
| 86 | + /** |
| 87 | + * 再次封装校验,对于异常直接抛出 |
| 88 | + * |
| 89 | + * @param param 参数 |
| 90 | + */ |
| 91 | + public static void check(Object param) throws IllegalArgumentException { |
| 92 | + Map<String, String> validateMap = BeanValidator.validateObject(param); |
| 93 | + if (Objects.nonNull(validateMap) && !validateMap.isEmpty()) { |
| 94 | + throw new RuntimeException(validateMap.values().iterator().next()); |
| 95 | + } |
94 | 96 | } |
95 | | - } |
96 | 97 | } |
0 commit comments