{"id":56485,"date":"2023-01-24T20:18:06","date_gmt":"2023-01-24T14:48:06","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=56485"},"modified":"2024-01-02T17:41:52","modified_gmt":"2024-01-02T12:11:52","slug":"customizing-validation-in-spring-boot-how-to-create-your-own-constraints","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/customizing-validation-in-spring-boot-how-to-create-your-own-constraints\/","title":{"rendered":"Customizing Validation in Spring Boot: How to Create Your Own Constraints"},"content":{"rendered":"<h1 class=\"graf graf--h3\" style=\"text-align: center;\"><strong>Customizing Validation in Spring Boot: How to Create Your Own Constraints\u00a0<\/strong><\/h1>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" class=\"graf-image aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1t_Nocct8eEJuBqcErpJumw.png\" data-image-id=\"1*t_Nocct8eEJuBqcErpJumw.png\" data-width=\"352\" data-height=\"111\" \/><\/figure>\n<p class=\"graf graf--p graf--hasDropCapModel graf--hasDropCap\"><span class=\"graf-dropCap\">By<\/span> developing unique constraint annotations and validator classes for Spring Boot, customized validation can be implemented.Here we can show more than one validation error messages<br \/>\nThe general steps to build a custom validator in Spring Boot are as follows:<\/p>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 0 : Add dependency in pom.xml<\/strong><\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" loading=\"lazy\" class=\"graf-image alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1f8AZTQIPtHxWNirjm7AJrw.png\" alt=\"\" width=\"469\" height=\"78\" data-image-id=\"1*f8AZTQIPtHxWNirjm7AJrw.png\" data-width=\"469\" data-height=\"78\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 1<\/strong>\u00a0<strong class=\"markup--strong markup--p-strong\">: Make your own constraint annotations<\/strong><br \/>\nThis annotation will be used to highlight fields that need to be validated.<br \/>\nThe annotation needs to be tagged with <a class=\"markup--anchor markup--p-anchor\" title=\"Twitter profile for @Constraint\" href=\"http:\/\/twitter.com\/Constraint\" target=\"_blank\" rel=\"noopener\" data-href=\"http:\/\/twitter.com\/Constraint\">@Constraint<\/a> and should be linked to a Validator class. Here I am using phone number validation.<\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" class=\"graf-image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1SS0Kf6ZAqOhcPd2Dj5G6Gg.png\" data-image-id=\"1*SS0Kf6ZAqOhcPd2Dj5G6Gg.png\" data-width=\"665\" data-height=\"241\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 2: The creation of a validator class<\/strong><br \/>\nThis class should include methods for validating the annotated fields and should implement the Constraint Validator interface.<\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" class=\"graf-image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1cKurqTR2gQR2a1jzrsKdZA.png\" data-image-id=\"1*cKurqTR2gQR2a1jzrsKdZA.png\" data-width=\"670\" data-height=\"264\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 3\u00a0: Fields that require validation should be annotated with a special constraint<\/strong><br \/>\nThis can be achieved by adding the custom constraint annotation to the<br \/>\nclass fields like here in contact.<\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" class=\"graf-image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1GU1B846dLQnWJTqDPVRKnw.png\" data-image-id=\"1*GU1B846dLQnWJTqDPVRKnw.png\" data-width=\"591\" data-height=\"387\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 4\u00a0: Use @Valid annotation in controller<\/strong><\/p>\n<p class=\"graf graf--p\">The <a class=\"markup--anchor markup--p-anchor\" title=\"Twitter profile for @Valid\" href=\"http:\/\/twitter.com\/Valid\" target=\"_blank\" rel=\"noopener\" data-href=\"http:\/\/twitter.com\/Valid\">@Valid<\/a> annotation from Spring can be used to force object validation by adding it to a method argument or request body.<br \/>\nIf any constraints are broken, Spring will automatically validate the object and deliver a list of problems.<\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" class=\"graf-image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1FQEf0tGNaI1rBhJlBUyIxQ.png\" data-image-id=\"1*FQEf0tGNaI1rBhJlBUyIxQ.png\" data-width=\"562\" data-height=\"132\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 5\u00a0: Make a class that will display error message along with certain other validation errors<\/strong><\/p>\n<p class=\"graf graf--p\">To get the error message properly create a class to get and bind the validation errors.We have to catch the error messages from exceptions and attach that with our own format of response and return the same to the client.<\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" class=\"graf-image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/12N2MI9tMrNAU6fJaZ2aaDA.png\" data-image-id=\"1*2N2MI9tMrNAU6fJaZ2aaDA.png\" data-width=\"724\" data-height=\"593\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 6: Make a custom handler to display validation error message<\/strong><\/p>\n<p class=\"graf graf--p\">Make a class that has @ControllerAdvice annotation at class level and extends ResponseEntityExceptionHandler class.Override the handleMethodArgumentNotValid() method to show valid error message.This will display more than one validation errors .<\/p>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" class=\"graf-image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1mkkJa4prrATTkjGD5M2EdQ.png\" data-image-id=\"1*mkkJa4prrATTkjGD5M2EdQ.png\" data-width=\"775\" data-height=\"70\" \/><\/figure>\n<figure class=\"graf graf--figure\"><img decoding=\"async\" class=\"graf-image\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1eu_I9Cb6Mkfz84IFX0fZUQ.png\" data-image-id=\"1*eu_I9Cb6Mkfz84IFX0fZUQ.png\" data-width=\"612\" data-height=\"439\" \/><\/figure>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">STEP 7\u00a0: Now you can test it locally\u00a0!<\/strong><\/p>\n<p class=\"graf graf--p\">You can create more validation like unique email,password match and so on\u2026.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Customizing Validation in Spring Boot: How to Create Your Own Constraints\u00a0 By developing unique constraint annotations and validator classes for Spring Boot, customized validation can be implemented.Here we can show more than one validation error messages The general steps to build a custom validator in Spring Boot are as follows: STEP 0 : Add dependency [&hellip;]<\/p>\n","protected":false},"author":1541,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":388},"categories":[446,1994],"tags":[5090],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56485"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1541"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=56485"}],"version-history":[{"count":5,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56485\/revisions"}],"predecessor-version":[{"id":59762,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56485\/revisions\/59762"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=56485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=56485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=56485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}