Validating resources using bean validation support
After introducing the request-payload data binding process, we must talk about validation.
Getting ready
The goal of this recipe is to show how to get Spring MVC to reject request body payloads that are not satisfying a bean validation (JSR-303) or not satisfying the constraints of a defined Spring validator implementation.
After the Maven and Spring configuration, we will see how to bind a validator to an incoming request, how to define the validator to perform custom rules, how to set up a JSR-303 validation, and how to handle the validation results.
How to do it…
- We added a Maven dependency to the hibernate validator:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> ...
Get Spring MVC: Designing Real-World Web Applications now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.