Bean Validation Using The Hibernate Validator
Explore how to implement bean validation in Spring Data JPA using the Hibernate Validator. Learn to apply common validation annotations like @NotNull and @Size on entity fields, validate REST API request bodies with @Valid, and enforce validations in the service layer using the Validator bean. By the end of this lesson, you will understand how to ensure data integrity and handle validation errors within your Spring-based REST APIs.
Add validation dependency
First, let’s add the validation dependency provided by Spring Boot— spring-boot-starter-validation—into our build.gradle file.
This will add the required hibernate-validator dependency to our todo appilcation, allowing us to express and validate constraints on the entity for request validation.
Entity setup for validation
Javax’s validation framework provides a few ...