Data Validation in Java: Overview of Tools and Libraries
Data validation (checking correctness) is one of the key stages in developing any application. Without reliable input data validation, an application becomes vulnerable to errors, incorrect behavior, and attacks. In Java, there are several proven approaches and libraries that allow you to implement validation quickly, flexibly, and with a minimum amount of boilerplate code.
In this article, we will look at the main tools for data validation in Java: the Bean Validation (Jakarta Validation) standard, its most popular implementation Hibernate Validator, as well as an alternative — Apache Commons Validator. We will also touch on manual validation for simple cases.
1. Bean Validation Standard (Jakarta Validation)
Bean Validation (formerly JSR 380, now Jakarta Validation) is a specification that defines a set of annotations for declarative validation of Java objects. The main idea: you describe validation rules directly in the class fields using annotations, and the validation engine automatically checks them.
Main Annotations
@NotNull— the value must not be null.@NotEmpty— the string, collection, or array must not be empty (and not null).@NotBlank— the string must not be empty or consist only of spaces.@Size(min, max)— limits the length of a string, or the size of a collection or array.@Min/@Max— numeric limits.@Pattern(regexp = "...")— validation against a regular expression.@Email— validation of email address format.
Usage Example
import jakarta.validation.constraints.*;
public class User { @NotBlank(message = "Name cannot be empty") @Size(min = 2, max = 50) private String name;
@Email(message = "Invalid email") @NotNull private String email;
@Min(18) @Max(120) private int age;
// getters and setters}To run validation, use the Validator object:
import jakarta.validation.*;import java.util.Set;
public class ValidationExample { public static void main(String[] args) { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator();
User user = new User(); user.setName("A"); user.setEmail("invalid-email"); user.setAge(10);
Set<ConstraintViolation<User>> violations = validator.validate(user); for (ConstraintViolation<User> violation : violations) { System.out.println(violation.getMessage()); } }}2. Hibernate Validator — Implementation of the Standard
Hibernate Validator is the reference implementation of the Bean Validation specification. It fully supports all Jakarta Validation annotations and adds its own extensions, such as @URL, @CreditCardNumber, @ISBN, and others.
Additional Features
- Custom validators — you can create your own annotations and validation logic.
- Validation groups — check different sets of rules depending on the context (e.g., creation vs. update).
- Integration with Spring Boot, Java EE / Jakarta EE out of the box.
Custom Validator Example
import jakarta.validation.Constraint;import jakarta.validation.Payload;import java.lang.annotation.*;
@Target({ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)@Constraint(validatedBy = PhoneValidator.class)public @interface ValidPhone { String message() default "Invalid phone number";