I'm new to the Spring Framework, and as a symptom, I want to keep my adoption of its Web MVC portions as simple as possible, so I'm using the annotation functions to work with Spring. In the past, I've used: int value = Integer.valueOf(request.getParameter("numberValue"))
to pull values from parameters - explicitly converting the String returned by getParameter()
.
Helpfully, I've noticed that when I use Spring's terminology:@RequestParameter("numberValue") int numVal
the conversion is handled automatically. This is nice, but a "black box" to me. I tried looking at questions on here or in the Spring documentation, but all that information deals with custom conversions (like Converter) for objects or formatting issues. All I want to know is how Spring handles primitive type conversions for @RequestParam
by default.
I've noticed that when I use Spring's terminology: @RequestParameter("numberValue") int numVal the conversion is handled automatically.
Here you are looking for Type Conversion
As per spring documentation given on this link
String-based values extracted from the request including request parameters, path variables, request headers, and cookie values may need to be converted to the target type of the method parameter or field (e.g., binding a request parameter to a field in an @ModelAttribute parameter) they’re bound to. If the target type is not String, Spring automatically converts to the appropriate type. All simple types such as int, long, Date, etc. are supported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With