Spring core 6.1 introduce three new methods in Validator interface
A method to validate objects individually:
default Errors validateObject(Object target)
Two factory methods to create validators
static <T> Validator forInstanceOf(Class<T> targetClass, BiConsumer<T,Errors> delegate)
static <T> Validator forType(Class<T> targetClass, BiConsumer<T,Errors> delegate)
Let's see example of these new methods
Examples
Using Validator#forInstanceOf
package com.logicbig.example;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import java.math.BigDecimal;
public class ValidatorForInstanceOfExample {
public static void main(String[] args) {
Validator validator = Validator.forInstanceOf(Number.class, (number, errors) -> {
if (number.doubleValue() < 0) {
errors.reject("number.negative", String.format("Number (%s) cannot be negative. Found: %s",
number.getClass(), number));
}
});
if (validator.supports(Integer.class)) {
Errors errors = validator.validateObject(Integer.valueOf(-1));
errors.getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.forEach(System.out::println);
}
if (validator.supports(BigDecimal.class)) {
Errors errors = validator.validateObject(BigDecimal.valueOf(-10));
errors.getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.forEach(System.out::println);
}
}
}
OutputNumber (class java.lang.Integer) cannot be negative. Found: -1 Number (class java.math.BigDecimal) cannot be negative. Found: -10
Using Validator#forType
package com.logicbig.example;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import java.math.BigDecimal;
public class ValidatorForTypeExample {
public static void main(String[] args) {
Validator validator = Validator.forType(Integer.class, (number, errors) -> {
if (number.doubleValue() < 0) {
errors.reject("number.negative", String.format("Number (%s) cannot be negative. Found: %s",
number.getClass(), number));
}
});
if (validator.supports(Integer.class)) {
Errors errors = validator.validateObject(Integer.valueOf(-1));
errors.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).forEach(System.out::println);
}
if (validator.supports(BigDecimal.class)) {
Errors errors = validator.validateObject(BigDecimal.valueOf(-10));
errors.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).forEach(System.out::println);
}
}
}
OutputNumber (class java.lang.Integer) cannot be negative. Found: -1
Example ProjectDependencies and Technologies Used: - spring-context 6.2.12 (Spring Context)
Version Compatibility: 6.1.0 - 6.2.12 Version compatibilities of spring-context with this example:
- 6.1.0
- 6.1.1
- 6.1.2
- 6.1.3
- 6.1.4
- 6.1.5
- 6.1.6
- 6.1.7
- 6.1.8
- 6.1.9
- 6.1.10
- 6.1.11
- 6.1.12
- 6.1.13
- 6.1.14
- 6.1.15
- 6.1.16
- 6.1.17
- 6.1.18
- 6.1.19
- 6.1.20
- 6.1.21
- 6.2.0
- 6.2.1
- 6.2.2
- 6.2.3
- 6.2.4
- 6.2.5
- 6.2.6
- 6.2.7
- 6.2.8
- 6.2.9
- 6.2.10
- 6.2.11
- 6.2.12
Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|