Tuesday, 13 April 2021

Bean Validation: configure validation error messages globally

By defining the message keys in 'ValidationMessages.properties' file, you can globally configure the validation error messages.

 

How can I get the message key?

Just go to the source code of the constraint. Default value of the message attribute is the message key for that constraint.

 

For example, 'javax.validation.constraints.Size.message' is the message key for the constraint @Size.

public @interface Size {

	String message() default "{javax.validation.constraints.Size.message}";

	......
	......
}

 

Follow below step-by-step procedure to define global validation error messages.

 

Step 1: Create ‘ValidationMessages.properties’ file under src/main/resources folder.

 

ValidationMessages.properties

javax.validation.constraints.Size.message= User Name '${validatedValue}' is not upto the requirement. Name must have atleast {min} characters

 

Step 2: Define Employee class like below.

 

Employee.java

package com.sample.app.model;

import javax.validation.constraints.Size;

public class Employee {

	private int id;

	@Size(min=5)
	private String name;

	public Employee(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

 

Step 3: Define App.java like below.

 

App.java

 

package com.sample.app;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.sample.app.model.Employee;

public class App {
	private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
	private static Validator validator = validatorFactory.getValidator();

	private static void validateBean(Employee emp) {
		System.out.println("************************************");
		Set<ConstraintViolation<Employee>> validationErrors = validator.validate(emp);

		if (validationErrors.size() == 0) {
			System.out.println("No validation errors....");
		}

		for (ConstraintViolation<Employee> violation : validationErrors) {
			System.out.println(violation.getPropertyPath() + "," + violation.getMessage());
		}
		System.out.println("");
	}

	public static void main(String args[]) {
		Employee emp1 = new Employee(1, "Ram");
		System.out.println("Validation Errors for emp1");
		validateBean(emp1);

		Employee emp2 = new Employee(2, "RamaKrishna");
		System.out.println("Validation Errors for emp2");
		validateBean(emp2);

	}
}

 

Run App.java, you will see below messages in console.

Validation Errors for emp1
************************************
name,User Name 'Ram' is not upto the requirement. Name must have atleast 5 characters

Validation Errors for emp2
************************************
No validation errors....

 

 

 

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment