Rajat Goel Rajat Goel - 18 days ago 5
Java Question

how to prevent user from entering decimal values in integer fields in REST APIs in spring in java

Spring is automatically converting Decimal values to Integers while using POST Method in REST APIs. While, I want to throw a custom exception if user enters Decimal values. Is there any easy way to do this.

Eg:: If user enters age = 4.3 , then Spring is converting it to 4.

Answer
if (age instanceof Integer) //dowork();
else //throw custom exception

consider age as type Object and then use the above code to do your testing.

Comments