java - JSR 349 验证 : ExecutableValidator. 静态方法的 validateParameters

标签 java validation bean-validation jsr349

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/

相关文章:

java - OkHttp 是否支持接受自签名 SSL 证书?

java - Stardog 数据加载和 Jena

正则表达式和 yup : Allow a certain special character, 但不能重复

validation - 使用扩展名和所有指示器的 XSD 验证错误

java - 未调用 Resteasy Bean 验证

java - Spring异常处理程序,无法访问请求属性

java - 如何从 JavaFX 中的 TextArea 获取文本并保存换行符?

wpf - 在 M-V-VM 中使用 IDataErrorInfo

java - 如何在数组/列表/集合的元素上使用 Java Bean validator (JSR-303/JSR-349)

spring - 使用属性文件中的自定义消息进行 Hibernate 验证