Empowering Businesses with Tech
Javax Validation
Javax Validation is used to validate an incoming request. We need to set some constraints on the fields we want and with the help of some annotations, we can validate the fields and allow only those instances in the database that passes the validation check.
Implementing Javax Validation:
​
-
We need to add a spring-boot-starter dependency for validation. This dependency internally uses javax-validation.
-
Then we will add “@NotNull” above the username variable in the Yoga User class to validate that the username should not be null.
-
Also, “@Email” above the email variable in Yoga User to validate the correct format of the email address.
-
And finally, in the Yoga User Controller, we will use the “@Valid” annotation to add the user method’s parameter. This will validate the object marked with the annotation.
Fig: We got an error when we put email in the not appropriate format.​