0

I have a web application that is using Jersey2 to publish various REST services.

In this project, I am using the javax.validation annotations to validate the various inputs. However, I have some custom data types (Money, Percentage) where I would like to provide min and max validation in some cases.

However, if I annotate the field with @Min or @Max, I get an error message that there is no validator, which is the expected behavior, because my object is a complex data type.

I have successfully implemented a custom annotation with validator that provides this functionality, but I would prefer to reuse the existing annotation and supply my own validator for this data type, but I can't find any documentation on how to do this. Is it possible? If so, how do I register my custom validator for my data type?

Nathan
  • 1,576
  • 8
  • 18

1 Answers1

0

It is possible to register your own validators for existing constraint annotations. You can do so by providing a list of such validators in META-INF/services/javax.validation.ConstraintValidator file. This would use the Service Loader mechanism to find your constraint validators and add them to the validation engine. For a more detailed explanation please check the next post - Adding custom constraint definitions via the Java service loader, and "Use standard constraints for non standard classes" section in particular.

mark_o
  • 2,052
  • 1
  • 12
  • 18
  • My ServiceLoader finds my classes but I am still getting the error: "HV000030: No validator could be found for type: math.Money." According to the stacktrace and the debugger, org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.findMatchingValidatorClass(ConstraintValidatorManager.java:193) identifies only the standard validators as candidates. My validator is never considered here. – Nathan Apr 12 '18 at 07:50