'How to add int validation on configuration value in Spring
In my Spring Boot
application, I have a class that uses a value from the configuration file:
@Service
public class Cat{
@Value("${cat.maxAge}")
private int maxAge;
....
I like to add validation on the Min
and Max
values, so the app will fail if some wrong values are in the config file.
Trying to do so:
@Service
public class Cat{
@Value("${cat.maxAge}")
@Max(value=10)
private int maxAge;
....
And IntelliJ doesn't know what @Max
is and doesn't recognize any package to import for it.
Is there a way to do it?
Solution 1:[1]
Make sure that you have the needed dependecy in you build.
If you are using gradle.build
the add this line to dependencies
like the following
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation:2.6.3'
If you are using make sure to add this dependency as the following
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.6.3</version>
</dependency>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | George |