1

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?

riorio
  • 6,500
  • 7
  • 47
  • 100

1 Answers1

0

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>
George
  • 2,292
  • 2
  • 10
  • 21
  • Thanks for the reply. Now the import is added `javax.validation.constraints.Max`, but when putting values outside of the min/max, the app is just ignoring the constraints and starts with no problem - while I was expecting it to give errors. – riorio Feb 23 '22 at 12:26
  • Am not sure that this is the issue as am not an expert with spring, But I do believe that you need to change the `@service`, this [question](https://stackoverflow.com/questions/44007492/enforce-constraints-on-value-annotated-field-in-spring-boot-application) might help. because the usage of @Min and @Max is very straightforward – George Feb 23 '22 at 12:45