/**
* 获取方法参数验证器
*
* @param parametric 参数
* @return 参数验证器
*/
protected Validator getValidator(final WrapperParametric parametric) {
//过滤掉了不验证的方法或泛型接口
if (validator == null || !parametric.getBoolean(VALIDATION_OPTION.getName(), validation)) {
return null;
}
try {
Method method = getPublicMethod(interfaceClass, parametric.getName());
//判断该方法上是有有验证注解
MethodDescriptor descriptor = beanDescriptor.getConstraintsForMethod(method.getName(), method.getParameterTypes());
return descriptor != null && descriptor.hasConstrainedParameters() ? validator : null;
} catch (NoSuchMethodException | MethodOverloadException e) {
return null;
}
}
/**
* Only accepts if method isn't parameterless and have at least one constraint.
*/
private boolean hasConstraints(ControllerMethod controllerMethod) {
Method method = controllerMethod.getMethod();
if (method.getParameterTypes().length == 0) {
logger.debug("method {} has no parameters, skipping", controllerMethod);
return false;
}
BeanDescriptor bean = bvalidator.getConstraintsForClass(controllerMethod.getController().getType());
if(bean == null) {
return false;
}
MethodDescriptor descriptor = bean.getConstraintsForMethod(method.getName(), method.getParameterTypes());
return descriptor != null && descriptor.hasConstrainedParameters();
}