..
This commit is contained in:
@@ -22,30 +22,30 @@ import java.util.stream.Collectors;
|
||||
@Slf4j
|
||||
public class ParamValidationProcessor extends AbstractProcessor {
|
||||
|
||||
protected ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
// protected ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
|
||||
@Override
|
||||
public void process(ActionContext ac) throws Throwable {
|
||||
Validator validator = factory.getValidator();
|
||||
Method method = ac.getMethod();
|
||||
Object[] methodArgs = ac.getMethodArgs();
|
||||
Parameter[] parameters = method.getParameters();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
Parameter parameter = parameters[i];
|
||||
boolean hasValidAnnotation = parameter.isAnnotationPresent(Valid.class);
|
||||
if (hasValidAnnotation) {
|
||||
Object argValue = methodArgs[i];
|
||||
if (argValue == null) {
|
||||
throw new MethodArgumentNotValidException(parameter.getName() + "不能为null");
|
||||
}
|
||||
|
||||
Set<ConstraintViolation<Object>> violations = validator.validate(argValue);
|
||||
if (!violations.isEmpty()) {
|
||||
String errMsg = violations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(","));
|
||||
throw new MethodArgumentNotValidException(errMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Validator validator = factory.getValidator();
|
||||
// Method method = ac.getMethod();
|
||||
// Object[] methodArgs = ac.getMethodArgs();
|
||||
// Parameter[] parameters = method.getParameters();
|
||||
// for (int i = 0; i < parameters.length; i++) {
|
||||
// Parameter parameter = parameters[i];
|
||||
// boolean hasValidAnnotation = parameter.isAnnotationPresent(Valid.class);
|
||||
// if (hasValidAnnotation) {
|
||||
// Object argValue = methodArgs[i];
|
||||
// if (argValue == null) {
|
||||
// throw new MethodArgumentNotValidException(parameter.getName() + "不能为null");
|
||||
// }
|
||||
//
|
||||
// Set<ConstraintViolation<Object>> violations = validator.validate(argValue);
|
||||
// if (!violations.isEmpty()) {
|
||||
// String errMsg = violations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(","));
|
||||
// throw new MethodArgumentNotValidException(errMsg);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
doNext(ac);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user