Search code examples
spring-bootconfigurationproperties

Spring ConfigurationProperties validation of a Double value


I have a property value that should range between 0 and 1. I like Spring's ConfigurationProperties to validate the property value.

So in my ConfigProperties class I added the @Validated annotation and wrote this:

@Min(0)
@Max(1)
Double fraction;

The strange thing is that the validation works in a manner that looks like flooring / roofing the value from the property file.

This is the outcome of different values I put in the conf file:

fraction=-2.1 -> Spring reports an error and stops (good!)

fraction=2.1 -> Spring reports an error and stops (good!)

fraction=-1.5 -> Spring doesn't report an error and starts (not good!)

fraction=1.5 -> Spring doesn't report an error and starts (not good!)

I also tried using the @Range annotation, but with the same outcomes


Solution

  • So here is the solution as described here:

       @DecimalMax("1.0") @DecimalMin("0.0")
        Double fraction;