Double Validation in Spring

I want to check the score, which has a value: 0.00 <= value <= 10.00
I used:
 -Model (Score):

 @Range(min = (long) 0.0, max = (long) 10.0)
 private double score;

-messages.properties:

Range.score.score=Please input 0<= score <=10

-servlet-context.xml:

   <beans:bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <beans:property name="basename" value="messages" />
    </beans:bean>

But value = 10.01 passed the test.
Please help me.

+4
source share
2 answers

I enable my proplem with:

 @DecimalMax("10.0") @DecimalMin("0.0") 
 private double score;

Thanks a lot @Kayaman, @hyness

+9
source

There are two problems. The first problem is that you are trying to specify the data type in the annotation attribute. You also do not need to specify a data type or fraction, this annotation contains only integers.

, , , BigDecimal double. BigDecimal doubleValue(), double.

@Range(min = 0, max = 10)
private BigDecimal score;
+1

Source: https://habr.com/ru/post/1629208/


All Articles