@DecimalMin does not validate whether input is numeric, results in InvalidFormatException

Viewed 362

According to the docs, @DecimalMin should check both whether the input is a number and if it is greater than the provided value, correct? Here is the definition:

The annotated element must be a number whose value must be higher or equal to the specified minimum.

I have a POJO, where I am trying to use the validation:

public class ConversionInputDto {

    @DecimalMin(value = "0.0", message = ERROR_INVALID_AMOUNT)
    private BigDecimal sourceAmount;

    // other variables, no-args constructor, getters

}

My controller has the @Validated annotation and the actual method looks like this:

@PostMapping("/convert")
public ConversionInputDto convertTo(@RequestBody @Valid ConversionInputDto conversionInputDto) {
    // ...
}

However, when I send a String payload like: "sourceAmount": "test", I get the following error:

"Invalid JSON input: Cannot deserialize value of type `java.math.BigDecimal` from String \"test\": not a valid representation; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException:

Should @DecimalMin not also ensure that the input is numeric, or have I missed something?

0 Answers
Related