Spring Validator Framework validates the input Java class properties. Validator class must implement org.springframework.validation.Validator and define the validator rules in the overridden method -
public void validate(Object o, Errors errors)
This method contains the validation rules. For example, for a Person class validation rules can be to verify if name, address properties of the incoming Person class are not null and not empty.
org.springframework.validation.ValidationUtils invokes the validation on the incoming Person class by using the Validator Implemention that defined the validation rules.
There are 2 ways to invoke validation on the incoming objects -
1) ValidationUtils.invokeValidator(personValidator, person, errors); - personValidator is the class implementing Validation interface and errors is the object of class BindException
2) personValidator.validate(person, errors);
Here is a sample implementation of the Spring Validation Framework
Person.java
package com.learning.com.springvalidation; public class Person { private String name; private int age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
PersonValidator.java
package com.learning.com.springvalidation; import org.springframework.validation.Errors; import org.springframework.validation.Validator; public class PersonValidator implements Validator { public boolean supports(Class<?> aClass) { return Person.class.isAssignableFrom(aClass); } public void validate(Object o, Errors errors) { if (o == null) { errors.reject("Person Object is Null"); } Person person = (Person) o; if (person.getName() == null) { errors.reject("Name Field Is Null"); } if (person.getAddress() == null) { errors.reject("Address Field Is Null"); } } }
PersonClient.java
package com.learning.com.springvalidation; import org.springframework.validation.BindException; import org.springframework.validation.ValidationUtils; public class PersonClient { public static void main(String args[]) { PersonValidator personValidator = new PersonValidator(); Person person = new Person(); BindException errors = new BindException(person, "person"); ValidationUtils.invokeValidator(personValidator, person, errors); System.out.println("Error - " + errors.getMessage()); } }
Output
"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.5\lib\idea_rt.jar=53398:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;C:\Kumar\Projects\learning\target\classes;C:\Users\bhatiak1\.m2\repository\org\springframework\spring-context\5.1.4.RELEASE\spring-context-5.1.4.RELEASE.jar;C:\Users\bhatiak1\.m2\repository\org\springframework\spring-aop\5.1.4.RELEASE\spring-aop-5.1.4.RELEASE.jar;C:\Users\bhatiak1\.m2\repository\org\springframework\spring-beans\5.1.4.RELEASE\spring-beans-5.1.4.RELEASE.jar;C:\Users\bhatiak1\.m2\repository\org\springframework\spring-core\5.1.4.RELEASE\spring-core-5.1.4.RELEASE.jar;C:\Users\bhatiak1\.m2\repository\org\springframework\spring-jcl\5.1.4.RELEASE\spring-jcl-5.1.4.RELEASE.jar;C:\Users\bhatiak1\.m2\repository\org\springframework\spring-expression\5.1.4.RELEASE\spring-expression-5.1.4.RELEASE.jar" com.learning.com.springvalidation.PersonClient Error - org.springframework.validation.BeanPropertyBindingResult: 2 errors Error in object 'person': codes [Name Field Is Null.person,Name Field Is Null]; arguments []; default message [null] Error in object 'person': codes [Address Field Is Null.person,Address Field Is Null]; arguments []; default message [null] Process finished with exit code 0
No comments:
Post a Comment