{"id":126412,"date":"2024-09-16T11:44:57","date_gmt":"2024-09-16T08:44:57","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=126412"},"modified":"2024-09-16T11:45:01","modified_gmt":"2024-09-16T08:45:01","slug":"spring-validator-validation-example","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html","title":{"rendered":"Spring Validator Validation Example"},"content":{"rendered":"<h2 class=\"wp-block-heading\">1. Introduction<\/h2>\n<p><a href=\"https:\/\/docs.spring.io\/spring-framework\/docs\/4.1.x\/spring-framework-reference\/html\/validation.html\" target=\"_blank\" rel=\"noreferrer noopener\">Spring Validation Framework<\/a> includes the <a href=\"https:\/\/docs.spring.io\/spring-framework\/docs\/current\/javadoc-api\/org\/springframework\/validation\/Validator.html\" target=\"_blank\" rel=\"noreferrer noopener\">Validator<\/a> interface that handles data validation. It is used to implement custom validation logic. In this example, I will create a Spring MVC rest controller and implement the Spring validator interface to validate the create request.<\/p>\n<h2 class=\"wp-block-heading\">2. Setup<\/h2>\n<p>In this step, I will create a gradle project along with <a href=\"https:\/\/projectlombok.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">lombok<\/a> and Junit libraries.<\/p>\n<p><span style=\"text-decoration: underline\"><em>build.gradle<\/em><\/span><\/p>\n<pre class=\"brush:plain\">plugins {\n\tid 'java'\n\tid 'org.springframework.boot' version '3.3.3'\n\tid 'io.spring.dependency-management' version '1.1.6'\n}\n\ngroup = 'org.zheng.demo.validator'\nversion = '0.0.1-SNAPSHOT'\n\njava {\n\ttoolchain {\n\t\tlanguageVersion = JavaLanguageVersion.of(17)\n\t}\n}\n\nconfigurations {\n\tcompileOnly {\n\t\textendsFrom annotationProcessor\n\t}\n}\n\n\nrepositories {\n\tmavenCentral()\n}\n\ndependencies {\n\timplementation 'org.springframework.boot:spring-boot-starter-web'\n\tcompileOnly 'org.projectlombok:lombok'\n\tannotationProcessor 'org.projectlombok:lombok'\n\ttestImplementation 'org.springframework.boot:spring-boot-starter-test'\n\ttestRuntimeOnly 'org.junit.platform:junit-platform-launcher'\n}\n\ntasks.named('test') {\n\tuseJUnitPlatform()\n}\n<\/pre>\n<h2 class=\"wp-block-heading\">3. Implement Spring Validator Interface<\/h2>\n<h3 class=\"wp-block-heading\"><a name=\"step31\"><\/a>3.1 UserData<\/h3>\n<p>In this step, I will create a <code>UserData<\/code> class which has three data fields: <code>userId<\/code>, <code>name<\/code>, and <code>email<\/code>. The <code>lombok<\/code> annotations: <code>@Data<\/code> and <code>@NoArgsConstructor<\/code> are used to reduce boilerplate code.<\/p>\n<p><span style=\"text-decoration: underline\"><em>UserData<\/em><\/span><\/p>\n<pre class=\"brush:java\">package org.zheng.demo.validator.model;\n\nimport lombok.Data;\nimport lombok.NoArgsConstructor;\n\n@Data\n@NoArgsConstructor\npublic class UserData {\t\n\tprivate String userId;\n\tprivate String name;\n\tprivate String email;\n}\n<\/pre>\n<h3 class=\"wp-block-heading\"><a name=\"step32\"><\/a>3.2 UserDataValidator<\/h3>\n<p>In this step, I will create a <code>UserDataValidator<\/code> class that implements the <code>Validator<\/code> interface from the <code>org.springframework.validation<\/code> package. It overrides both <code>supports<\/code> and <code>validate<\/code> methods for the <code>UserData<\/code> class defined in step <a href=\"#step31\">3.1<\/a>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>UserDataValidator.java<\/em><\/span><\/p>\n<pre class=\"brush:java;highlight:[12,17]\">package org.zheng.demo.validator.model;\n\nimport org.springframework.stereotype.Component;\nimport org.springframework.validation.Errors;\nimport org.springframework.validation.ValidationUtils;\nimport org.springframework.validation.Validator;\n\n@Component\npublic class UserDataValidator implements Validator {\n\n\t@Override\n\tpublic boolean supports(Class&lt;?&gt; clazz) {\n\t\treturn UserData.class.isAssignableFrom(clazz);\n\t}\n\n\t@Override\n\tpublic void validate(Object target, Errors errors) {\n\t\tValidationUtils.rejectIfEmptyOrWhitespace(errors, \"userId\", \"missing userId.\");\n\t\tValidationUtils.rejectIfEmptyOrWhitespace(errors, \"name\", \"missing name.\");\n\t}\n}\n<\/pre>\n<ul class=\"wp-block-list\">\n<li>Line 12: the <code>supports<\/code> method checks if the <code>validator<\/code> can validate instances of the given class. In this example, it supports the validation at the <code>UserData<\/code> class.<\/li>\n<li>Line 17: the <code>validate<\/code> method performs the actual validation on the given <code>target<\/code> object and records any validation errors in the given <code>errors<\/code> object. In this example, it utilizes the <code>ValidationUtils.rejectIfEmptyOrWhitespace<\/code> to validate the <code>userId<\/code> and <code>name<\/code> fields.<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\">3.3 UserDataValidatorTest<\/h3>\n<p>In this step, I will create a <code>UserDataValidatorTest<\/code> class to test the validation defined in step <a href=\"#step32\">3.2<\/a>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>UserDataValidatorTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java;highlight:[11,18,29]\">package org.zheng.demo.validator.model;\n\nimport static org.junit.jupiter.api.Assertions.assertFalse;\nimport static org.junit.jupiter.api.Assertions.assertTrue;\n\nimport org.junit.jupiter.api.Test;\nimport org.springframework.validation.BeanPropertyBindingResult;\nimport org.springframework.validation.Errors;\n\nclass UserDataValidatorTest {\n\tprivate UserDataValidator testClass = new UserDataValidator();\n\n\t@Test\n\tvoid test_invalidUser() {\n\t\tUserData user = new UserData();\n\t\tErrors error = new BeanPropertyBindingResult(user, \"userData\");\n\n\t\ttestClass.validate(user, error);\n\t\tassertTrue(error.hasErrors());\n\t}\n\t\n\t@Test\n\tvoid test_validUser() {\n\t\tUserData user = new UserData();\n\t\tuser.setUserId(\"userId\");\n\t\tuser.setName(\"Zheng\");\n\t\tErrors error = new BeanPropertyBindingResult(user, \"userData\");\n\n\t\ttestClass.validate(user, error);\n\t\tassertFalse(error.hasErrors());\n\t}\n}\n<\/pre>\n<ul class=\"wp-block-list\">\n<li>Line 11: create an instance of the <code>UserDataValidator<\/code> class.<\/li>\n<li>Line 18, 29: validate the <code>userData<\/code> object.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">4. User RestController<\/h2>\n<p>In this step, I will create a <code>UserRestController<\/code> class that utilizes the <code>UserDataValidator<\/code> defined in step <a href=\"#step32\">3.2<\/a> to validate the user request when creating a user.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><span style=\"text-decoration: underline\"><em>UserReestController.java<\/em><\/span><\/p>\n<pre class=\"brush:java;highlight:[20,30]\">package org.zheng.demo.validator.controller;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.http.HttpStatus;\nimport org.springframework.http.ResponseEntity;\nimport org.springframework.validation.BeanPropertyBindingResult;\nimport org.springframework.validation.Errors;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.PostMapping;\nimport org.springframework.web.bind.annotation.RequestBody;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\nimport org.zheng.demo.validator.model.UserData;\nimport org.zheng.demo.validator.model.UserDataValidator;\n\n@RestController\n@RequestMapping(\"\/api\")\npublic class UserRestController {\n\t@Autowired \n\tprivate UserDataValidator ud;\n\n\t@GetMapping(\"\/ping\")\n\tpublic String ping() {\n\t\treturn \"ok\";\n\t}\n\n\t@PostMapping(\"\/user\")\n\tpublic ResponseEntity&lt;?&gt; createUser(@RequestBody UserData user) {\n\t\tErrors errors = new BeanPropertyBindingResult(user, \"userData\");\n\t\tud.validate(user, errors);\n\t\tif (errors.hasErrors()) {\n\t\t\treturn ResponseEntity.badRequest().body(errors.getAllErrors());\n\t\t}\n\t\t\/\/continue with other tasks, for now, just return for demo\n\t\treturn new ResponseEntity&lt;&gt;(user, HttpStatus.OK);\n\t}\n}\n<\/pre>\n<ul class=\"wp-block-list\">\n<li>Line 20: injects a <code>UserDataValidator<\/code> instance.<\/li>\n<li>Line 30: invokes the <code>validate<\/code> method to validate the create request user data.<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\">4.1 UserRestControllerTest<\/h3>\n<p>In this step, I will create a <code>UserRestControllerTest<\/code> and test via <code>@SpringBootTest<\/code> and <code>@AutoConfigureMockMvc<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>UserRestControllerTest.java<\/em><\/span><\/p>\n<pre class=\"brush:java;highlight:[36,44,54]\">package org.zheng.demo.validator.controller;\n\nimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;\nimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;\nimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;\nimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;\n\nimport org.junit.jupiter.api.Test;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.test.web.servlet.MockMvc;\nimport org.zheng.demo.validator.model.UserData;\n\nimport com.fasterxml.jackson.databind.ObjectMapper;\n\n@SpringBootTest\n@AutoConfigureMockMvc\nclass UserRestControllerTest {\n\n\t@Autowired\n\tprivate MockMvc mockMvc;\n\n\t@Autowired\n\tprivate ObjectMapper ob;\n\n\t@Test\n\tvoid test_ping() throws Exception {\n\t\tmockMvc.perform(get(\"\/api\/ping\")).andExpect(status().isOk()).andExpect(content().string(\"ok\"));\n\t}\n\n\t@Test\n\tvoid test_create_invalid() throws Exception {\n\t\tUserData user = new UserData();\n\n\t\tmockMvc.perform(post(\"\/api\/user\").contentType(\"application\/json\").content(ob.writeValueAsString(user)))\n\t\t\t\t.andExpect(status().isBadRequest());\n\t}\n\n\t@Test\n\tvoid test_create_invalid_missingName() throws Exception {\n\t\tUserData user = new UserData();\n\t\tuser.setUserId(\"test\");\n\t\tmockMvc.perform(post(\"\/api\/user\").contentType(\"application\/json\").content(ob.writeValueAsString(user)))\n\t\t\t\t.andExpect(status().isBadRequest());\n\t}\n\n\n\t@Test\n\tvoid test_create_ok() throws Exception {\n\t\tUserData user = new UserData();\n\t\tuser.setUserId(\"test\");\n\t\tuser.setName(\"testName\");\n\t\tmockMvc.perform(post(\"\/api\/user\").contentType(\"application\/json\").content(ob.writeValueAsString(user)))\n\t\t\t\t.andExpect(status().isOk());\n\t}\n}\n<\/pre>\n<ul class=\"wp-block-list\">\n<li>Line 36: validate the create request with an empty <code>userId<\/code> and <code>name<\/code> will receive the bad_request code.<\/li>\n<li>Line 44: validate the create request with a missing <code>name<\/code> will receive the bad_request code.<\/li>\n<li>Line 54: validate the create request with both <code>userId<\/code> and <code>name<\/code> will receive the ok status.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">5. Demo<\/h2>\n<h3 class=\"wp-block-heading\">5.1 Junit Tests<\/h3>\n<p>Execute the junit test and capture the test results.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/tests.jpg\"><img decoding=\"async\" width=\"470\" height=\"259\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/tests.jpg\" alt=\"Spring Validator Interface Test Results\" class=\"wp-image-126432\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/tests.jpg 470w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/tests-300x165.jpg 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/a><figcaption class=\"wp-element-caption\">Figure 1 Test Results<\/figcaption><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\">5.2 Rest Test<\/h3>\n<p>In this step, I will start the spring boot application and confirm the server is up at port 8080 via the server log.<\/p>\n<p><span style=\"text-decoration: underline\"><em>server log<\/em><\/span><\/p>\n<pre class=\"brush:plain\">\n  .   ____          _            __ _ _\n \/\\\\ \/ ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\\n( ( )\\___ | '_ | '_| | '_ \\\/ _` | \\ \\ \\ \\\n \\\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )\n  '  |____| .__|_| |_|_| |_\\__, | \/ \/ \/ \/\n =========|_|==============|___\/=\/_\/_\/_\/\n\n :: Spring Boot ::                (v3.3.3)\n\n2024-09-14T05:00:35.697-05:00  INFO 23180 --- [spring-validator-demo] [           main] o.z.d.v.SpringValidatorDemoApplication   : Starting SpringValidatorDemoApplication using Java 17.0.11 with PID 23180 (C:\\MaryTools\\workspace\\spring-validator-demo\\bin\\main started by azpm0 in C:\\MaryTools\\workspace\\spring-validator-demo)\n2024-09-14T05:00:35.705-05:00  INFO 23180 --- [spring-validator-demo] [           main] o.z.d.v.SpringValidatorDemoApplication   : No active profile set, falling back to 1 default profile: \"default\"\n2024-09-14T05:00:36.672-05:00  INFO 23180 --- [spring-validator-demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)\n2024-09-14T05:00:36.688-05:00  INFO 23180 --- [spring-validator-demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]\n2024-09-14T05:00:36.688-05:00  INFO 23180 --- [spring-validator-demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat\/10.1.28]\n2024-09-14T05:00:36.771-05:00  INFO 23180 --- [spring-validator-demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[\/]       : Initializing Spring embedded WebApplicationContext\n2024-09-14T05:00:36.776-05:00  INFO 23180 --- [spring-validator-demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1015 ms\n2024-09-14T05:00:37.148-05:00  INFO 23180 --- [spring-validator-demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '\/'\n2024-09-14T05:00:37.158-05:00  INFO 23180 --- [spring-validator-demo] [           main] o.z.d.v.SpringValidatorDemoApplication   : Started SpringValidatorDemoApplication in 1.826 seconds (process running for 2.222)\n<\/pre>\n<p>Once the server is started, navigate to a web browser and validate with the &#8220;<a href=\"http:\/\/localhost:8080\/api\/ping\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/localhost:8080\/api\/ping<\/a>&#8220;. It should return the &#8220;ok&#8221; string.<\/p>\n<p>In this step, I will use the <a href=\"https:\/\/docs.thunderclient.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Thunder Client plugin<\/a> in Visual Studio Code to test the create user request.<\/p>\n<p>Figure 2 shows the bad request error when the request body is missing.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-test.jpg\"><img decoding=\"async\" width=\"653\" height=\"542\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-test.jpg\" alt=\"Rest Validator Test 1\" class=\"wp-image-126446\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-test.jpg 653w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-test-300x249.jpg 300w\" sizes=\"(max-width: 653px) 100vw, 653px\" \/><\/a><figcaption class=\"wp-element-caption\">Figure 2 Rest Test Validation for Missing Body<\/figcaption><\/figure>\n<\/div>\n<p>Capture the generated curl command for no body request:<\/p>\n<p><span style=\"text-decoration: underline\"><em>curl command for no body content request<\/em><\/span><\/p>\n<pre class=\"brush:plain\">curl  -X POST \\\n  'http:\/\/localhost:8080\/api\/user' \\\n  --header 'Accept: *\/*' \\\n  --header 'User-Agent: Thunder Client (https:\/\/www.thunderclient.com)' \\\n  --header 'Content-Type: application\/json'<\/pre>\n<p>Figure 3 shows the missing <code>name<\/code> validation.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-test2.jpg\"><img decoding=\"async\" width=\"657\" height=\"530\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-test2.jpg\" alt=\"Rest Validation Test 2\" class=\"wp-image-126447\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-test2.jpg 657w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-test2-300x242.jpg 300w\" sizes=\"(max-width: 657px) 100vw, 657px\" \/><\/a><figcaption class=\"wp-element-caption\">Figure 3 Missing Name Request<\/figcaption><\/figure>\n<\/div>\n<p>Capture the generated curl command for missing the name field request:<\/p>\n<p><span style=\"text-decoration: underline\"><em>curl command for missing name<\/em><\/span><\/p>\n<pre class=\"brush:plain\">curl  -X POST \\\n  'http:\/\/localhost:8080\/api\/user' \\\n  --header 'Accept: *\/*' \\\n  --header 'User-Agent: Thunder Client (https:\/\/www.thunderclient.com)' \\\n  --header 'Content-Type: application\/json' \\\n  --data-raw '{\"userId\":\"test\"}'<\/pre>\n<p>Figure 4 shows a valid request.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-testOk.jpg\"><img decoding=\"async\" width=\"662\" height=\"521\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-testOk.jpg\" alt=\"Rest Validation Valid Request\" class=\"wp-image-126450\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-testOk.jpg 662w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/rest-testOk-300x236.jpg 300w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/><\/a><figcaption class=\"wp-element-caption\">Figure 4 Valid Request<\/figcaption><\/figure>\n<\/div>\n<p>Capture the generated curl command for a valid request:<\/p>\n<p><span style=\"text-decoration: underline\"><em>curl command for a valid request<\/em><\/span><\/p>\n<pre class=\"brush:plain\">curl  -X POST \\\n  'http:\/\/localhost:8080\/api\/user' \\\n  --header 'Accept: *\/*' \\\n  --header 'User-Agent: Thunder Client (https:\/\/www.thunderclient.com)' \\\n  --header 'Content-Type: application\/json' \\\n  --data-raw '{\n  \"userId\": \"test\",\n  \"name\": \"zheng\"\n}'\n<\/pre>\n<h2 class=\"wp-block-heading\">6. Conclusion<\/h2>\n<p>In this example, I created a spring boot application for a Rest controller. The controller utilized the Spring validator interface to validate the request.<\/p>\n<h2 class=\"wp-block-heading\">7. Download<\/h2>\n<p>This was an example of a gradle project which includes the Spring Validator interface.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/09\/spring-validator-demo.zip\"><strong>Spring Validator Validation Example<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction Spring Validation Framework includes the Validator interface that handles data validation. It is used to implement custom validation logic. In this example, I will create a Spring MVC rest controller and implement the Spring validator interface to validate the create request. 2. Setup In this step, I will create a gradle project along &hellip;<\/p>\n","protected":false},"author":128892,"featured_media":240,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[854,150,292],"class_list":["post-126412","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-spring-boot","tag-spring-mvc","tag-validators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Validator Validation Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Interested to learn more about spring validator interface? Then check out our detailed example on spring validator interface validation example!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Validator Validation Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Interested to learn more about spring validator interface? Then check out our detailed example on spring validator interface validation example!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-16T08:44:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-16T08:45:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Mary Zheng\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mary Zheng\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html\"},\"author\":{\"name\":\"Mary Zheng\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/33e795ab61de7fab61ed89b4de1668f5\"},\"headline\":\"Spring Validator Validation Example\",\"datePublished\":\"2024-09-16T08:44:57+00:00\",\"dateModified\":\"2024-09-16T08:45:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html\"},\"wordCount\":527,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"keywords\":[\"Spring Boot\",\"Spring MVC\",\"Validators\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html\",\"name\":\"Spring Validator Validation Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"datePublished\":\"2024-09-16T08:44:57+00:00\",\"dateModified\":\"2024-09-16T08:45:01+00:00\",\"description\":\"Interested to learn more about spring validator interface? Then check out our detailed example on spring validator interface validation example!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"spring-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-validator-validation-example.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring Validator Validation Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/33e795ab61de7fab61ed89b4de1668f5\",\"name\":\"Mary Zheng\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/cropped-Mary-Zheng-96x96.jpg\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/cropped-Mary-Zheng-96x96.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/cropped-Mary-Zheng-96x96.jpg\",\"caption\":\"Mary Zheng\"},\"description\":\"Mary graduated from the Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She worked as a lead Software Engineer where she led and worked with others to design, implement, and monitor the software solution.\",\"sameAs\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/mary-zheng\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Validator Validation Example - Java Code Geeks","description":"Interested to learn more about spring validator interface? Then check out our detailed example on spring validator interface validation example!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html","og_locale":"en_US","og_type":"article","og_title":"Spring Validator Validation Example - Java Code Geeks","og_description":"Interested to learn more about spring validator interface? Then check out our detailed example on spring validator interface validation example!","og_url":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2024-09-16T08:44:57+00:00","article_modified_time":"2024-09-16T08:45:01+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","type":"image\/jpeg"}],"author":"Mary Zheng","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Mary Zheng","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html"},"author":{"name":"Mary Zheng","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/33e795ab61de7fab61ed89b4de1668f5"},"headline":"Spring Validator Validation Example","datePublished":"2024-09-16T08:44:57+00:00","dateModified":"2024-09-16T08:45:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html"},"wordCount":527,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","keywords":["Spring Boot","Spring MVC","Validators"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html","url":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html","name":"Spring Validator Validation Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","datePublished":"2024-09-16T08:44:57+00:00","dateModified":"2024-09-16T08:45:01+00:00","description":"Interested to learn more about spring validator interface? Then check out our detailed example on spring validator interface validation example!","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","width":150,"height":150,"caption":"spring-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/spring-validator-validation-example.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring Validator Validation Example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/33e795ab61de7fab61ed89b4de1668f5","name":"Mary Zheng","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/cropped-Mary-Zheng-96x96.jpg","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/cropped-Mary-Zheng-96x96.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/cropped-Mary-Zheng-96x96.jpg","caption":"Mary Zheng"},"description":"Mary graduated from the Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She worked as a lead Software Engineer where she led and worked with others to design, implement, and monitor the software solution.","sameAs":["https:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/mary-zheng"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/126412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/128892"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=126412"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/126412\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/240"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=126412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=126412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=126412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}