This isn't something that would be done on the validation level.
When you enter something in a form and you submit that form, the browser will send an HTTP request with your <input>
fields serialized and sent as request parameters. Spring then, based on the data type of your bean's field, tries to convert from a String
request parameter value to a Long
type. If it can't do that because the String is not a Long
, it will throw exceptions (NumberFormatException
) and respond with a 400 error code.
You can validate this on the (HTML5) client side with
<input name="userId" type="number">
Or use
<input type="text" pattern="\d*" />
if you don't want decimal numbers.