Tuesday 6 April 2021

Bean Validation: @Null: Element must be null

You can apply @Null annotation on any element, it makes sure that the element is null.

 

Example

@Null

private Object config;

 

Supported Types

It can be applied on any type.

 

Where can I apply this annotation?

a.   METHOD

b.   FIELD

c.    ANNOTATION_TYPE

d.   CONSTRUCTOR

e.   PARAMETER

f.     TYPE_USE

 

Employee.java

package com.sample.app.model;

import javax.validation.constraints.Null;

public class Employee {

	private int id;

	private String name;

	private int age;
	
	@Null
	private Object config;

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

	public int getId() {
		return id;
	}

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

	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;
	}

}

 

Test.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 Test {
	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, null, 28, 123);
		System.out.println("Validation errors on bean emp1");
		validateBean(emp1);

		Employee emp2 = new Employee(2, "", 59, "Hello");
		System.out.println("Validation errors on bean emp2");
		validateBean(emp2);

		Employee emp3 = new Employee(3, " ", 9, true);
		System.out.println("Validation errors on bean emp3");
		validateBean(emp3);

		Employee emp4 = new Employee(4, "Sailaja", 24, null);
		System.out.println("Validation errors on bean emp4");
		validateBean(emp4);
	}
}

 

Output

Validation errors on bean emp1
************************************
config,must be null

Validation errors on bean emp2
************************************
config,must be null

Validation errors on bean emp3
************************************
config,must be null

Validation errors on bean emp4
************************************
No validation errors....

 

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment