使用Javax.Validation校验字段完备性 最近在做项目过程中,发现为了检验一个类里面各个字段是否完整,需要写很多check函数,例如:
1 2 3 4 if (pLane==null ){ String errorMsg="车道的值是null" ; throw new XXXException(errorMsg); }
这些校验的函数就写在主计算的方法中,感觉不是很好,于是尝试使用Javax.Validation模块优化一下:
首先导入包:
1 2 3 4 5 6 7 8 9 10 11 <dependency > <groupId > javax.validation</groupId > <artifactId > validation-api</artifactId > <version > 1.1.0.Final</version > </dependency > <dependency > <groupId > org.hibernate</groupId > <artifactId > hibernate-validator</artifactId > <version > 5.4.1.Final</version > </dependency >
然后对需要检查的类添加注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import org.hibernate.validator.constraints.NotBlank;import javax.validation.constraints.DecimalMin;import javax.validation.constraints.Max;import javax.validation.constraints.Min;import javax.validation.constraints.NotNull;public class User { @NotNull(message = "用户ID不能为空") private Integer id; @NotBlank(message = "用户名不能为空") private String username; @Min(value = 1,message = "低于最小值") @Max(value = 10,message = "超过最大值") private Double salary; }
然后编写测试工具:ValidationUtils
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 import java.util.HashMap;import java.util.Map;import java.util.Set;import javax.validation.ConstraintViolation;import javax.validation.Validation;import javax.validation.Validator;import javax.validation.groups.Default;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 (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; } }
以及ValidationResult
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 import java.util.Map;public class ValidationResult { private boolean hasErrors; 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 + '}' ; } }
(上述内容参考:https://blog.csdn.net/icannotdebug/article/details/78708202)
然后写测试样例 就可以测试了。
例如 :
1 2 3 4 5 User user=new User(null ,null ,11.0 ); ValidationResult validationResult=ValidationUtils.validateEntity(user); if (validationResult.isHasErrors){ throw new ....Exception }