Validation数据校验规范使用
# pom
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 代码使用
# 创建通用校验返回结果类:ValidationResult
package cn.ok96.util;
import java.util.Map;
public class ValidationResult {
//校验结果是否有错
private boolean hasErrors;
//校验错误信息 key: property value : errorMessage
private Map<String,String> errorMsg;
public boolean isHasErrors() {
return hasErrors;
}
public void setHasErrors(boolean hasErrors) {
this.hasErrors = hasErrors;
}
public Map<String, String> getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(Map<String, String> errorMsg) {
this.errorMsg = errorMsg;
}
@Override
public String toString() {
return "ValidationResult [hasErrors=" + hasErrors + ", errorMsg="
+ errorMsg + "]";
}
}
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
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
# 创建校验工具类:ValidationUtils
package cn.ok96.util;
import org.springframework.util.CollectionUtils;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 校验工具类
*
* @author lizhilong
*/
public class ValidationUtils {
private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
public static <T> ValidationResult validateEntity(T obj) {
ValidationResult result = new ValidationResult();
Set<ConstraintViolation<T>> set = validator.validate(obj, Default.class);
// if( CollectionUtils.isNotEmpty(set) ){
if (set != null && set.size() != 0) {
result.setHasErrors(true);
Map<String, String> errorMsg = new HashMap<String, String>();
for (ConstraintViolation<T> cv : set) {
errorMsg.put(cv.getPropertyPath().toString(), cv.getMessage());
}
result.setErrorMsg(errorMsg);
}
return result;
}
public static <T> ValidationResult validateProperty(T obj, String propertyName) {
ValidationResult result = new ValidationResult();
Set<ConstraintViolation<T>> set = validator.validateProperty(obj, propertyName, Default.class);
if (set != null && set.size() != 0) {
result.setHasErrors(true);
Map<String, String> errorMsg = new HashMap<String, String>();
for (ConstraintViolation<T> cv : set) {
errorMsg.put(propertyName, cv.getMessage());
}
result.setErrorMsg(errorMsg);
}
return result;
}
public static <T> Map<String, StringBuilder> validate(T object, Class<?>... groups) {
Map<String, StringBuilder> errorMap = new HashMap<>(16);
if (groups == null) {
groups = new Class[]{Default.class};
}
Set<ConstraintViolation<T>> set = validator.validate(object, groups);
if (CollectionUtils.isEmpty(set)) {
return null;
}
String property;
for (ConstraintViolation<T> c : set) {
// 这里循环获取错误信息,可以自定义格式
property = c.getPropertyPath().toString();
if (errorMap.get(property) != null) {
errorMap.get(property).append(",").append(c.getMessage());
} else {
StringBuilder sb = new StringBuilder();
sb.append(c.getMessage());
errorMap.put(property, sb);
}
}
return errorMap;
}
}
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
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
# 实体类添加校验规则
package cn.ok96.modules.api.entity;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;
import java.io.Serializable;
import javax.validation.constraints.NotNull;
@Data
public class Okmo implements Serializable {
//设备 ID
@Id
@NotNull(message = "id不能为空")
private String id;
//名称
@NotBlank(message="名称不能为空")
@Column(name = "name")
private String name;
//型号
@NotNull(message = "型号不能为空")
@Column(name = "model")
private String model;
}
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
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
# 代码中使用
Okmo ok=new Okmo();
//校验实体类
ValidationResult vr = ValidationUtils.validateEntity(ok);
if (vr.isHasErrors()) {
StringBuffer msg = new StringBuffer();
for (String key : vr.getErrorMsg().keySet()) {
msg.append("key:").append(key).append(",value:").append(vr.getErrorMsg().get(key)).append(";");
}
System.out.println(msg);
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
上次更新: 2023/03/13, 03:16:04