BeanValidation 1.1 规范定义了 API ExecutableValidator.validateParameters
以验证对给定方法的参数施加的所有约束。
但是 API 要求它传递一个对象实例,在该实例上调用要验证的方法:
/**
* Validates all constraints placed on the parameters of the given method.
*
* @param <T> the type hosting the method to validate
* @param object the object on which the method to validate is invoked
* @param method the method for which the parameter constraints is validated
* @param parameterValues the values provided by the caller for the given method's
* parameters
* @param groups the group or list of groups targeted for validation (defaults to
* {@link Default})
* @return a set with the constraint violations caused by this validation;
* will be empty if no error occurs, but never {@code null}
* @throws IllegalArgumentException if {@code null} is passed for any of the parameters
* or if parameters don't match with each other
* @throws ValidationException if a non recoverable error happens during the
* validation process
*/
<T> Set<ConstraintViolation<T>> validateParameters(T object,
Method method,
Object[] parameterValues,
Class<?>... groups);
我的问题是如何验证静态方法调用?例如,下面定义的 Foo.bar
方法的调用:
public class Foo {
public static void bar(@NotNull String str) {...}
}
最佳答案
Bean Validation 1.1 不支持静态方法。来自 Requirements on classes to be validated :
Objects hosting constraints and expecting to be validated by Bean Validation providers must fulfill the following requirements:
- [...]
- Static fields and static methods are excluded from validation.
某些实现者可能会在将来支持它作为增强功能(例如 HV-606 用于 Hibernate Validator),但规范本身并不支持。 2.0 draft of the specification 仍然明确不支持它.
关于java - JSR 349 验证 : ExecutableValidator. 静态方法的 validateParameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42602366/