Spring- Spring Validation

Spring Validation

JSR-303 standardizes validation constraint declaration and metadata for the Java platform. Using this API, you annotate domain model properties with declarative validation constraints and the runtime enforces them

JSR-303 allows you to define declarative validation constraints against such properties:

public class Car {

   @NotNull
   private String manufacturer;

   @NotNull
   @Size(min = 2, max = 14)
   private String licensePlate;

   @Min(2)
   private int seatCount;

   // ...
}

When an instance of this class is validated by a JSR-303 Validator, these constraints will be enforced.

For general information on JSR-303/JSR-349, see the Bean Validation website. For information on the specific capabilities of the default reference implementation, see the Hibernate Validator documentation.


Spring Expression Language (SpEL)

The Spring Expression Language (SpEL) is a powerful expression language that supports querying and manipulating an object graph at runtime. It can be used with XML or annotation-based Spring configurations.

There are several operators available in the language:

Type Operators
Arithmetic +, -, *, /, %, \^, div, mod
Relational <, >, ==, !=, <=, >=, lt, gt, eq, ne, le, ge
Logical and, or, not, &&, ||, !
Conditional ?:
Regex matches

Syntax

#{ Some Operation }


1.Arithmetic Operators

@Value("#{19 + 1}") // 20
private double add; 
 
@Value("#{'String1 ' + 'string2'}") // "String1 string2"
private String addString; 
 
@Value("#{20 - 1}") // 19
private double subtract;
 
@Value("#{10 * 2}") // 20
private double multiply;
 
@Value("#{36 / 2}") // 19
private double divide;
 
@Value("#{36 div 2}") // 18, the same as for / operator
private double divideAlphabetic; 
 
@Value("#{37 % 10}") // 7
private double modulo;
 
@Value("#{37 mod 10}") // 7, the same as for % operator
private double moduloAlphabetic; 
 
@Value("#{2 ^ 9}") // 512
private double powerOf;
 
@Value("#{(2 + 2) * 2 + 9}") // 17
private double brackets;

Relational and Logical Operators

@Value("#{1 == 1}") // true
private boolean equal;
 
@Value("#{1 eq 1}") // true
private boolean equalAlphabetic;
 
@Value("#{1 != 1}") // false
private boolean notEqual;
 
@Value("#{1 ne 1}") // false
private boolean notEqualAlphabetic;
 
@Value("#{1 < 1}") // false
private boolean lessThan;
 
@Value("#{1 lt 1}") // false
private boolean lessThanAlphabetic;
 
@Value("#{1 <= 1}") // true
private boolean lessThanOrEqual;
 
@Value("#{1 le 1}") // true
private boolean lessThanOrEqualAlphabetic;
 
@Value("#{1 > 1}") // false
private boolean greaterThan;
 
@Value("#{1 gt 1}") // false
private boolean greaterThanAlphabetic;
 
@Value("#{1 >= 1}") // true
private boolean greaterThanOrEqual;
 
@Value("#{1 ge 1}") // true
private boolean greaterThanOrEqualAlphabetic;

Using Regex in SpEL

@Value("#{'100' matches '\\d+' }") // true
private boolean validNumericStringResult;
 
@Value("#{'100fghdjf' matches '\\d+' }") // false
private boolean invalidNumericStringResult;
 
@Value("#{'valid alphabetic string' matches '[a-zA-Z\\s]+' }") // true
private boolean validAlphabeticStringResult;
 
@Value("#{'invalid alphabetic string #$1' matches '[a-zA-Z\\s]+' }") // false
private boolean invalidAlphabeticStringResult;
 
@Value("#{someBean.someValue matches '\d+'}") // true if someValue contains only digits
private boolean validNumericValue;