Java Bean Validation with @NotNull, @Size and @Pattern
Learn how to use @NotNull, @NotBlank, @Size, and @Pattern annotations in Java Bean Validation. These annotations help ensure that the data entered by users meets specific validation rules before processing or storing it in the application.
Code Explanation (Step-by-Step)
@NotNull
- Ensures the field is not null.
- The value must exist.
@NotBlank
- Ensures the field is not null.
- Removes leading and trailing spaces.
- Rejects empty strings and blank values.
@Size(min = 4, max = 20)
- Defines minimum and maximum length.
- Employee name must contain between 4 and 20 characters.
@Pattern(regexp = “[a-zA-Z0-9]+$”)
- Allows only letters and numbers.
- Special characters are not permitted.
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
public class EmployeeDetail {
@NotNull(message = "empName should not be null")
@NotBlank(message = "empName should not be blank")
@Size(min = 4, max = 20,
message = "empName length should be between 4 and 20 characters")
@Pattern(regexp = "[a-zA-Z0-9]+$",
message = "empName can contain only letters and numbers")
private String empName;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
What Are Validation Annotations in Java?
Validation annotations in Java are used to validate data entered by users. They help enforce rules on object fields such as ensuring values are not null, not blank, within a specific size, or match a particular pattern.
In this example, we validate student information using validation annotations.
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
public class Student {
@NotNull(message = "Student ID should not be null")
private Integer studentId;
@NotBlank(message = "Student name should not be blank")
@Size(min = 3, max = 30,
message = "Student name length should be between 3 and 30 characters")
private String studentName;
@NotBlank(message = "Email should not be blank")
@Email(message = "Invalid email format")
private String email;
@Pattern(regexp = "^[6-9][0-9]{9}$",
message = "Invalid mobile number")
private String mobileNumber;
// Getters and Setters
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
}
Output :-
Email = “abcgmail.com”
Result: Validation Failed
Reason: Invalid email format
Here are some Java-related programs that will help you understand Java concepts better: