|
@@ -1,27 +1,29 @@
|
|
|
package thyyxxk.webserver.utils;
|
|
|
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import thyyxxk.webserver.config.exception.BizException;
|
|
|
+import thyyxxk.webserver.config.exception.ExceptionEnum;
|
|
|
|
|
|
import javax.validation.ConstraintViolation;
|
|
|
-import javax.validation.Validator;
|
|
|
+import javax.validation.Validation;
|
|
|
+import javax.validation.ValidatorFactory;
|
|
|
import java.util.Set;
|
|
|
|
|
|
-@Component
|
|
|
-@RequiredArgsConstructor
|
|
|
public class ValidatorUtils {
|
|
|
+ private static final ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
|
|
|
|
|
|
- private final Validator validator;
|
|
|
+ public static void validate(Object object) {
|
|
|
+ Set<ConstraintViolation<Object>> violations = validatorFactory.getValidator().validate(object);
|
|
|
|
|
|
- public void validate(Object object) {
|
|
|
- Set<ConstraintViolation<Object>> violations = validator.validate(object);
|
|
|
+ int index = 1;
|
|
|
|
|
|
if (!violations.isEmpty()) {
|
|
|
- StringBuilder errorMsg = new StringBuilder("Validation failed:");
|
|
|
+ StringBuilder errorMsg = new StringBuilder();
|
|
|
for (ConstraintViolation<Object> violation : violations) {
|
|
|
- errorMsg.append("\n").append(violation.getMessage());
|
|
|
+ errorMsg.append("\n").append(index).append("、").append(violation.getMessage());
|
|
|
+ index++;
|
|
|
}
|
|
|
- throw new IllegalArgumentException(errorMsg.toString());
|
|
|
+ throw new BizException(ExceptionEnum.LOGICAL_ERROR, errorMsg.toString());
|
|
|
}
|
|
|
}
|
|
|
}
|