如何避免 Hibernate Validator ConstraintDeclarationException?

How to avoid Hibernate Validator ConstraintDeclarationException?

我有基于 Spring 4 的 WebService,并且我正在使用 Hibernate Validator(超越 MethodValidationPostProcessor)。我的问题是我有 ClientService 接口及其实现。因此,我将 Bean Validation 约束放在实现上,它迫使我将这些约束放在接口上(抛出 ConstraintDeclarationException)(或同时放在两者中)。

关于这个我想知道两件事:

  1. 为什么会这样?为什么它会强迫我对界面施加所有约束?是什么原因?
  2. 有没有办法只在实现中加入约束?

提前致谢!问候

回答你的第一个问题。该行为在 Bean 验证规范第 4.5.5 节中指定。 Method constraints in inheritance hierarchies. Basically the rule is that a method's preconditions (as represented by parameter constraints) must not be strengthened in sub types. This is the so called Liskov substitution principle

要回答您的第二个问题,目前无法对实施施加限制 class。 HV-872 建议将这些规则的可配置放宽作为 Hibernate Validator 特定功能来实现,但尚未实现。