The “data-validation-example” branch of the “openlmis-example” repository now contains examples of the two main options for Spring validation. Specifically, it includes:
JSR 349 and JPA based annotations used by Spring MVC as well as Spring Data Rest endpoints. The examples include a custom-annotation applied at the class level used to validate the state of multiple properties as they relate to one another. The same approach could be used to validate individual fields in ways unsupported by the built-in annotations.
A Validator used by Spring MVC as well as Spring Data Rest endpoints.
To quote from one of my commit-messages:
It should be noted that the Validator-based approach seems rare. The preponderance of tutorials and books instead demonstrate the use of annotations. One book I read even goes so far as to advise against the validator-based approach. (Its reason seemed more a matter of preference than principle. It suggests that, just as Spring Annotation-based configuration is cleaner than the XML-based alternative, so too are annotations cleaner than the Validator pattern.)
To elaborate on the above point: even the Spring team seems to prefer annotations over the Validator pattern. This is manifest in their documentation, and corroborated by the fact that bug https://jira.spring.io/browse/DATAREST-524 has been left open for over a year and marked as a low priority.
That isn’t to imply that I’ve yet developed a preference. If we don’t have reason to opt otherwise, though, it argues for the newer annotation-based option. With either approach, we still have to iron out how error/validation responses will be returned in a consistent manner.
If folks have thoughts or preferences, please chime in.