I will go ahead and assume a few things about your problem.
It is not a type-cast
problem. Spring has default converters that can easily convert from a String
to some primitive type.
Now what you are facing is I think a null assigment to primitive type
problem. Suppose the name of the property
that's causing the problem is named primitiveProperty
. Now, the request-paramters could include a parameter named primitiveProperty
with an empty-String value, or some value that cannot be converted to a number. If the type of the primitiveProperty
is String, it can assign the value of that parameter to it without any problem.
If the type of the primitiveProperty
is int
, long
or some other primitive type that cannot have a null
value, a problem occurs. When Spring converts the empty-string or a non-numeric string valued request-param named primitiveProperty
, it cannot do so since that string can't be converted to a valid int
or long
value. So it is converted to null. Now, when Spring tries to assign that null value to a property that cannot have a null
value (any primitve type), you get an Exception
. If you are getting an empty-string as your request-param, you can replace the troublesome property in your domain object with its equivalent wrapper class
(int
with Integer
, long
with Long
and so on). If you are getting a non-numeric value from your view, well, make sure that you don't get a non-numeric value.