Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Spring MVC convert @RequestParam values?

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.

like image 693
WannabeCoder Avatar asked Apr 22 '15 17:04

WannabeCoder


1 Answers

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.

like image 175
Dev Avatar answered Sep 28 '22 03:09

Dev