Monday 5 April 2021

Bean Validation: Positive: Number must be > 0

You can apply @Positive annotation on a number type field, it makes sure that given element is assigned with value > 0.

 

Example

@Positive

private int id;

 

Supported Types

a.   BigDecimal

b.   BigInteger

c.    byte

d.   short

e.   int

f.     long

g.   float

h.   double

i.     All the wrapper classes of primitive types

 

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 java.util.Date;

import javax.validation.constraints.Positive;

public class Employee {

	@Positive
	private int id;

	private String name;

	public Date joinedDate;

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

	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 Date getJoinedDate() {
		return joinedDate;
	}

	public void setJoinedDate(Date joinedDate) {
		this.joinedDate = joinedDate;
	}

}

 

Test.java

package com.sample.app;

import java.util.Calendar;
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, "Rama Krishna", Calendar.getInstance().getTime());
		System.out.println("Validation errors on bean emp1");
		validateBean(emp1);

		Employee emp2 = new Employee(-2, "Siva", Calendar.getInstance().getTime());
		System.out.println("Validation errors on bean emp2");
		validateBean(emp2);
		
		Employee emp3 = new Employee(0, "Siva553", Calendar.getInstance().getTime());
		System.out.println("Validation errors on bean emp3");
		validateBean(emp3);
	}
}

 

Output

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

Validation errors on bean emp2
************************************
id,must be greater than 0

Validation errors on bean emp3
************************************
id,must be greater than 0


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment