引言:在常见的工程中,一般是在Controller中校验入参,校验入参的方式有多种,这里介绍的使用hibernate-validate来验证,其中分为手动和自动校验,自动校验可以联合spring,使用@Valid注解,以及 BindingResult类来自动收集消息,这里介绍的如何自动的,随时随地的调用工具类,校验指定类或注定属性是否符合规则,将错误封装。
1. 添加 hibernate-validate依赖
<!--hibernate validate--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.16.Final</version> </dependency>
2. 接收处理结果,以及输出格式化的一个实体类
package com.stylefeng.guns.core.util; import org.apache.commons.lang3.StringUtils; import java.text.MessageFormat; import java.util.Map; /** * @author ztkj-hzb * @Date 2019/8/28 11:26 * @Description 实体校验结果 */ public class ValidationResult { /** * 是否有异常 */ private boolean hasErrors; /** * 异常消息记录 */ private Map<String, String> errorMsg; /** * 获取异常消息组装 * * @return */ public String getMessage() { if (errorMsg == null || errorMsg.isEmpty()) { return StringUtils.EMPTY; } StringBuilder message = new StringBuilder(); errorMsg.forEach((key, value) -> { message.append(MessageFormat.format("{0}:{1} \r\n", key, value)); }); return message.toString(); } 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 + ‘}‘; } }
3. 创建工具类,提供公共方法校验,返回结果
package com.stylefeng.guns.core.util; import org.apache.commons.collections.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 ztkj-hzb * @Date 2019/8/28 11:21 * @Description 实体校验工具类 */ public class ValidateUtil { private ValidateUtil() { } /** * 验证器 */ private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); /** * 校验实体,返回实体所有属性的校验结果 * * @param obj * @param <T> * @return */ public static <T> ValidationResult validateEntity(T obj) { //解析校验结果 Set<ConstraintViolation<T>> validateSet = validator.validate(obj, Default.class); return buildValidationResult(validateSet); } /** * 校验指定实体的指定属性是否存在异常 * * @param obj * @param propertyName * @param <T> * @return */ public static <T> ValidationResult validateProperty(T obj, String propertyName) { Set<ConstraintViolation<T>> validateSet = validator.validateProperty(obj, propertyName, Default.class); return buildValidationResult(validateSet); } /** * 将异常结果封装返回 * * @param validateSet * @param <T> * @return */ private static <T> ValidationResult buildValidationResult(Set<ConstraintViolation<T>> validateSet) { ValidationResult validationResult = new ValidationResult(); if (CollectionUtils.isNotEmpty(validateSet)) { validationResult.setHasErrors(true); Map<String, String> errorMsgMap = new HashMap<>(); for (ConstraintViolation<T> constraintViolation : validateSet) { errorMsgMap.put(constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage()); } validationResult.setErrorMsg(errorMsgMap); } return validationResult; } }
原文地址:https://www.cnblogs.com/duguxiaobiao/p/11533505.html
时间: 2024-11-07 19:05:36