JSR 303 Bean Validation is the specification of the Java API for JavaBean validation in Java EE and Java SE. Simply put it provides an easy way of ensuring that the properties of your JavaBean(s) have the right values in them.
Prior to JSR 303, we would required to handle the validation using if-else statements.
In this POST we will do following.
- We will create customer bean, add various JSR 303 validation annotation to its.
- Will create REST endpoint where in request body will be customer bean.
- Around aspect will be created which will validate the beans before controller gets called.
Java Code
Customer.javaRestController
DataValidator
DataValidator Aspect
Dependencies
Here we have provided hibernate-validator as implementation provider for JSR303, you guys can use other implementation provider also.
Output using curl
curl -i -X POST -H "Content-Type:application/json" http://localhost:8082/cust -d '{"fName":"12345", "lName":"1"}'
GIT Url
Cheers – Happy learning 🙂
Ketan Gote
Comments
Post a Comment