BeanValidatorUtils 工具类

Validator工具类

Posted by Steven on 2019-03-29
Estimated Reading Time 2 Minutes
Words 356 In Total
Viewed Times

使用

1
String validStr = BeanValidatorUtils.validate(queryQuestionnaireRequestVo, BeanValidatorUtils.OutputType.ALL_STRING);

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

import org.springframework.util.CollectionUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* Created by Steven on 2019-12-03.
*
* @author Steven
* @version 1.0
*/


public class BeanValidatorUtils {
public BeanValidatorUtils() {
}

public static <T> String validate(T object, Class... groups) {
return validate(object, BeanValidatorUtils.OutputType.SIMPLE_STRING, groups);
}

public static <T> String validate(T object, BeanValidatorUtils.OutputType outputType, Class... groups) {
if (outputType == null) {
outputType = BeanValidatorUtils.OutputType.SIMPLE_STRING;
}

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<T>> constraintViolations = validator.validate(object, groups);
if (CollectionUtils.isEmpty(constraintViolations)) {
return "";
} else {
switch (outputType) {
case MAP:
return convertErrorMsg(constraintViolations);
case ALL_STRING:
return convertErrorMsgAllString(constraintViolations);
case SIMPLE_STRING:
return convertErrorMsgSimpleString(constraintViolations);
default:
return convertErrorMsgSimpleString(constraintViolations);
}
}
}

private static <T> String convertErrorMsg(Set<ConstraintViolation<T>> cvs) {
Map<String, StringBuilder> errorMap = new HashMap();
Iterator var3 = cvs.iterator();

while (var3.hasNext()) {
ConstraintViolation<T> cv = (ConstraintViolation) var3.next();
String property = cv.getPropertyPath().toString();
if (errorMap.get(property) != null) {
((StringBuilder) errorMap.get(property)).append("," + cv.getMessage());
} else {
StringBuilder sb = new StringBuilder();
sb.append(cv.getMessage());
errorMap.put(property, sb);
}
}

return errorMap.toString();
}

private static <T> String convertErrorMsgAllString(Set<ConstraintViolation<T>> cvs) {
StringBuilder sb = new StringBuilder();

ConstraintViolation cv;
for (Iterator var3 = cvs.iterator(); var3.hasNext(); sb.append(cv.getMessage())) {
cv = (ConstraintViolation) var3.next();
if (sb.length() != 0) {
sb.append(",");
}
}

return sb.toString();
}

private static <T> String convertErrorMsgSimpleString(Set<ConstraintViolation<T>> cvs) {
StringBuilder sb = new StringBuilder();
Iterator var3 = cvs.iterator();
if (var3.hasNext()) {
ConstraintViolation<T> cv = (ConstraintViolation) var3.next();
sb.append(cv.getMessage());
}

return sb.toString();
}

public static enum OutputType {
MAP,
ALL_STRING,
SIMPLE_STRING;

private OutputType() {
}
}
}


如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !