Sunday 14 March 2021

Hibernate-validator: Hello World application

In this post, I am going to show you an example of bean validation API annotations @Min, @Size.

 

JSR 380 specification provides @Min, @Size annotation to validate the bean properties.

 

Below table summarizes these annotations.

 

Annotation

Description

@Min

If you annotate any number field with this annotation, then the element must be greater than or equal to the specified minimum value.

@Size

The annotated element size must be within given boundaries.

 

public class Employee {

	@Min(value=1)
	private int id;
	
	@Size(min=5, max=30)
	private String firstName;
	
	@Size(min=5, max=30)
	private String lastName;
	
	.....
	.....

}

 

For example,

 

@Min(value=1)

private int id;

I annotated id with @Min(value=1), this annotation make sure that the value of the id is >=1, else it generated validation error.

 

@Size(min=5, max=30)

private String firstName;

I annotated firstName with @Size(min=5, max=30), this annotation make sure that the length of the field firstName is minimum 5 character and maximum 30 character.

 

Find the below working application.

 

Employee.java

package com.sample.model;

import javax.validation.constraints.Min;
import javax.validation.constraints.Size;

public class Employee {

	@Min(value=1)
	private int id;
	
	@Size(min=5, max=30)
	private String firstName;
	
	@Size(min=5, max=30)
	private String lastName;
	
	public Employee(int id, String firstName, String lastName) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public int getId() {
		return id;
	}

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

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

}

Test.java

package com.sample.test;

import java.util.Set;

import javax.validation.*;
import javax.validation.ValidatorFactory;

import com.sample.model.Employee;

public class Test {
	public static void main(String args[]) {
		ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
		Validator validator = validatorFactory.getValidator();

		Employee emp = new Employee(-1, "Hareesh", "Ram");

		Set<ConstraintViolation<Employee>> validationErrors = validator.validate(emp);

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

When I ran above application, I seen below messages in the console.

id,must be greater than or equal to 1
lastName,size must be between 5 and 30

Since the fields id, firstName are violating the minimum requirements specified by using @Id, @Size annotations, validator generates these error messages.




 

Previous                                                    Next                                                    Home

No comments:

Post a Comment