Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal working of Springs's @RequestParam annotation

In Spring, the two following statements are, if I'm not mistaken, identical:

@RequestParam("type") String type
@RequestParam String type

How can spring know the variable name of 'type' (second version). I was under the impression that this information was removed from the class files unless compiled with the -g flag (include debug information).

like image 602
Tobias Avatar asked Jul 31 '11 18:07

Tobias


People also ask

What is the use of @RequestParam annotation in Spring?

@RequestParam annotation enables spring to extract input data that may be passed as a query, form data, or any arbitrary custom data.

What is @RequestParam annotation in Spring boot?

Spring @RequestParam annotation is used to fetch the value of a parameter in the form request. In Spring MVC, "request parameters" map to query parameters, form data.

What does @RequestParam do in Spring boot?

@RequestParam is a Spring annotation used to bind a web request parameter to a method parameter. It has the following optional elements: defaultValue - used as a fallback when the request parameter is not provided or has an empty value. name - name of the request parameter to bind to.

What is the use of @RequestParam in Spring MVC?

In Spring MVC, the @RequestParam annotation is used to read the form data and bind it automatically to the parameter present in the provided method. So, it ignores the requirement of HttpServletRequest object to read the provided data.


1 Answers

The short version of this is that apparently the parameter names are being compiled in, if they weren't, you'd get an exception indicating that Spring MVC couldn't deduce the parameter name. That is, parameter names aren't always stored in the bytecode, but it seems like if they are, Spring will find them, if not, you need to specify them when you add the @RequestParam annotation.

Other details are available on this similar question and it's answers.


In 3.0.5.RELEASE, these annotations are processed in HandlerMethodInvoker.resolveHandlerArguments and it appears that if no value is supplied, Spring uses RequestParam.value(). This can return the empty string.

Further down, Spring uses HandlerMethodInvoker.resolveRequestParam, and inside there, if the parameter name is empty, it invokes HandlerMethodINvoker.getRequiredParameterName with MethodParameter methodParam as an argument:

718     private String getRequiredParameterName(MethodParameter methodParam) {
719     String name = methodParam.getParameterName();
720     if (name == null) {
721         throw new IllegalStateException(
722                 "No parameter name specified for argument of type [" + methodParam.getParameterType().getName() +
723                         "], and no parameter name information found in class file either.");
724     }
725     return name;
726 }

Note that here it tries to pull the information from methodParam, which, if we back up the tree, we see that resolveHandlerArguments actually creates a new MethodParameter for each argument that it processes. Inside MethodParameter, we can take a look at getParameterName():

276 public String getParameterName() {
277     if (this.parameterNameDiscoverer != null) {
278         String[] parameterNames = (this.method != null ?
279                 this.parameterNameDiscoverer.getParameterNames(this.method) :
280                 this.parameterNameDiscoverer.getParameterNames(this.constructor));
281         if (parameterNames != null) {
282             this.parameterName = parameterNames[this.parameterIndex];
283         }
284         this.parameterNameDiscoverer = null;
285     }
286     return this.parameterName;
287 }

So this uses something called a ParameterNameDiscoverer, but this is an interface and my trace isn't showing which implementation it's using, there are a few. Looking at LocalVariableTableParameterNameDiscoverer.getParameterNames we end up calling a LocalVariableTableParameterNameDiscoverer.ParameterNameDiscoveringVisitor as part of an org.objectweb.asm.ClassReader, which as far as I can tell tries to read the parameter name out of the bytecode.

like image 55
Mark Elliot Avatar answered Oct 16 '22 04:10

Mark Elliot