We use Spring MVC, Jackson for json, Hibernate ORM. We need to add front end validations. I came across hibernate validator.
We have a set of Domain classes [DO classes] annotated with JPA. We have another set of POJO classes [DTO classes] annotated for Json binding.
We want to implement infrastructure that does the following:
- DTO classes refer to the DO class constraints [where applicable] so that the truth is one place.
- Generate json schema , again by reusing the JPA annotations.
- Validate input to the rest calls.
For example, instead of the following:
PersonDTO{
@NotNull
@Size(min=2, max=60)
private String firstName;
}
How about:
PersonDTO{
@MapsTo(com.xyz.domain.PersonDO.firstName.Size) // referring to the JPA annotation
private String firstName;
}
And then, a custom validator figures out the constraints to uphold by looking at the JPA annotation.
Is the strategy I outlined a typical approach? Any feedback or opinions is appreciated. Can you point me to relevant any articles? Thank you.