How does Spring MVC convert @RequestParam values?

I'm new to the Spring Framework, and as a sign, I want to keep my MVC web parts as simple as possible, so I use annotation functions to work with Spring. I used to use:
int value = Integer.valueOf(request.getParameter("numberValue"))
output values ​​from parameters - explicitly convert the string returned by getParameter() .
Useful, I noticed that when I use Spring terminology:
@RequestParameter("numberValue") int numVal
conversion is processed automatically. This is good, but the black box is for me. I tried to look at the questions here or in the Spring documentation, but all this information is about custom conversions (like a converter) for objects or formatting problems. All I want to know is how, by default, Spring handles primitive type conversions for @RequestParam .

+6
source share
1 answer

I noticed that when I use Spring terminology: @RequestParameter ("numberValue") int numVal the conversion is processed automatically.

Here you are looking for type conversion

According to Spring documentation provided in 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 (for example, binding the request parameter to a field in @ModelAttribute) to which they are bound. If the target type is not String, Spring is automatically converted to the corresponding type. All simple types are supported, such as int, long, Date, etc.

+8
source

Source: https://habr.com/ru/post/985699/


All Articles