How to avoid Hibernate Validator ConstraintDeclarationException?

995 views Asked by At

I have my WebService based on Spring 4, and I am using Hibernate Validator (beyond MethodValidationPostProcessor). My problem is that I have my ClientService interface, and its implementation. So I put Bean Validation constraints on the implementation and it forces me to put that constraints on the interface (throwing ConstraintDeclarationException) (or in both of them).

I want to know two things about this:

  1. Why does it work like this? Why does it force me to put all constraints on the interface? What is the reason?
  2. Is there any way to put the constraints only in the implementation?

Thanks in advance! Regards

1

There are 1 answers

0
Hardy On BEST ANSWER

To answer your first question. The behavior is specified in the Bean Validation specification section 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.

To answer your second question, there is no way at the moment to place constraints on the implementation class. There is HV-872 which suggests to implement a configurable relaxation of these rules as a Hibernate Validator specific feature, but it is not implemented yet.