I started validating a method parameter with: if notValid then throw exception,
Then I realize that I can create my own annotations to validate parameters, and some minutes later I was looking for a library that already have those simple annotations for validating numbers for example.
I discover Jakarta Bean Validation (ex javax.validation),
so I added the following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${spring-boot.version}</version>
</dependency>
I started using it by annotating a method parameter.
public static String toString(
final BigDecimal numberToFormat,
@PositiveOrZero final int scale,
final char decimalSeparator,
final char groupingSeparator,
final NumberFormatPattern numberFormatPattern) {
String pattern = getPattern(scale, numberFormatPattern);
return format(numberToFormat, decimalSeparator, groupingSeparator, pattern);
}
Starting a JUnit test and passing -1 as scale value, I was expecting an exception to be thrown, but nothing happened.
class NumberFormatUtilsTest {
void _toString_INVALID_SCALE() {
BigDecimal number = NumberUtils.toBigDecimal("123.456");
assertThrows(ConstraintViolationException.class, () -> NumberFormatUtils.toString(number, -1, ',', '.', DECIMALS_ALWAYS));
}
}
Looking into documentation I see:
Note that declaring these constraints does not automatically cause their validation when the concerned methods are invoked.
Why someone should add a constraint without executing the validation?
No, I don't have, because as I said, I was expecting to be automatic, and not that I have to configure the bean validation
I am sorry that it does not work likes what you expect . It requires to configure Bean Validation and trigger the validation through the Bean Validation API. Also it does not support validating on the static method too. Quote from this as below :
Constraints may only be applied to instance methods, i.e. declaring constraints on static methods is not supported. Depending on the interception facility you use for triggering method validation, additional restrictions may apply, e.g. with respect to the visibility of methods supported as target of interception. Refer to the documentation of the interception technology to find out whether any such limitations exist.
Good news is spring boot already auto configure it and do all the necessary integration with Bean Validation for you, but you have to make it as a spring bean and enable the validation feature by annotating it with @Validated
. So change to the following :
@Service
@Validated
public class NumberFormatUtils{
public String toString(
final BigDecimal numberToFormat,
@PositiveOrZero final int scale,
final char decimalSeparator,
final char groupingSeparator,
final NumberFormatPattern numberFormatPattern)
}
}
And to use it , you have to get the NumberFormatUtils
bean instance from the spring context by @Autowired
for example , but not simply create the instance by yourself.