mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
ValidationUtil.validateAndThrowFirst 异常信息添加属性名
This commit is contained in:
parent
10671aa580
commit
f58d25dd8d
@ -19,6 +19,7 @@ package org.dromara.hutool.extra.validation;
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
|
||||
import jakarta.validation.*;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@ -74,11 +75,15 @@ public class ValidationUtil {
|
||||
* @throws ValidationException 校验不通过,则报 ValidationException 异常,调用者进行捕获,直接响应给前端用户
|
||||
*/
|
||||
public static void validateAndThrowFirst(final Object object, final Class<?>... groups)
|
||||
throws ValidationException {
|
||||
throws ValidationException {
|
||||
final Set<ConstraintViolation<Object>> constraintViolations = validate(object, groups);
|
||||
if (CollUtil.isNotEmpty(constraintViolations)) {
|
||||
final ConstraintViolation<Object> constraint = constraintViolations.iterator().next();
|
||||
throw new ValidationException(constraint.getMessage());
|
||||
if (StrUtil.contains(constraint.getMessageTemplate(), "jakarta.validation.constraints")) {
|
||||
throw new ValidationException(constraint.getPropertyPath() + constraint.getMessage());
|
||||
} else {
|
||||
throw new ValidationException(constraint.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user