In the tutorial about @Entity and @Table annotation in JPA, I mentioned to you the concept of Java Bean, they are POJOs with at least one constructor with no parameters. We often use Java Bean to contain information of an object. To ensure that the object information contained in the Java Bean matches our needs, Java introduces the Java Bean Validation spec that makes it possible to validate the object’s information easily. How is it in details? We will learn about Bean Validation in Java in this tutorial.
First, I will create a new Maven project as an example:
To work with Bean Validation, you need to declare the library implement this spec Hibernate Validator:
1 2 3 4 5 |
<dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.1.0.Alpha5</version> </dependency> |
You will see that the API library defines the spec that is also included in the Maven Dependencies section of the project.
If you work with J2SE desktop applications, you need to declare an additional dependency as the Unified Expression Language (EL) as follows:
1 2 3 4 5 |
<dependency> <groupId>org.glassfish</groupId> <artifactId>javax.el</artifactId> <version>3.0.1-b11</version> </dependency> |
This library is used to evaluate the expression in the error message if we use it.
Now I have a Student class as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava.beanvalidation; public class Student { private String name; private int age; 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; } } |
Suppose now I need to store the information of the students with this Student class, which the student’s name is not null and the age of the student cannot be less than 18.
To satisfy this condition, I will use Bean Validation to define constraints when users want to add a new student as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.huongdanjava.beanvalidation; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; public class Student { @NotNull(message = "Name cannot be null") private String name; @Min(value = 18, message = "Age should not be less than 18") private int age; 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; } } |
As you can see, here I use 2 annotations of Bean Validation @NotNull and @Min to define constraints for all student information. The message attribute in these two annotations is used to show to the user which value of the field is not correct.
You can find many other annotations in the spec page of the Bean Validation, here I only get these 2 annotations for example.
Now I will write an example to see how validation will happen.
First, I need to initialize ValidatorFactory object to retrieve Validator object as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.beanvalidation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; public class Application { public static void main(String[] args) { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); } } |
Suppose I have a student with the following information:
1 2 3 |
Student student = new Student(); student.setName("Khanh"); student.setAge(12); |
Then, use the validate() method of the Validator object:
1 2 3 4 |
Set<ConstraintViolation<Student>> violations = validator.validate(student, Student.class); for (ConstraintViolation<Student> violation : violations) { System.err.println(violation.getMessage()); } |
you will see the following result:
Because my age only set 12, this field is not right.