Suppose I have the following code snippet:
@RequestMapping(method = RequestMethod.GET)
public List<Article> getArticles(@RequestParam int offset,
@RequestParam int limit) {
...
}
How can Spring match the HTTP query parameters to the right formal parameters, when the parameter name is not explicitly stated as an annotation parameter?
Does it suppose the formal parameter names are always present in bytecode?
As I understand, it does not always have to be so. The formal parameter names can be retrieved from bytecode only when:
a) the class file has been compiled with -parameters javac option
b) the class file has been compiled with -g (or -g:vars) javac option, which adds debug information including the true variable names (since the formal parameters are stored as the first local variables of method)
You can use QueryParameter to ignore form values, while you'd want to use RequestParameter if you need to read the values from a form post. RequestParameter may return more values than QueryParameter, especially if the request is a form post.
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.
The @javax.ws.rs.QueryParam annotation allows you to inject individual URI query parameters into your Java parameters. For example, let's say we wanted to query a customer database and retrieve a subset of all customers in the database.
@QueryParam Binds the value(s) of a HTTP query parameter to a resource method parameter, resource class field, or resource class bean property. Pay attention here, it's query parameter which is bound to resource method's parameter (i.e. parameter in query string)
To find the answer to your question, I built my Spring REST app with maven
and using the following command:
mvn clean install -X
My build.log
contained the following debug information:
[DEBUG] Command line options:
... -g -nowarn -target 1.8 -source 1.8 -encoding UTF-8
As we can see, the -g
option is present by default, which generates debug information, including local variables.
To prove otherwise, I updated the maven-compiler-plugin
config with the following option:
-g:none
Config example:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-g:none</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
After that, my web service started to throw an error:
java.lang.IllegalArgumentException: Name for argument type [java.lang.String] not
available, and parameter name information not found in class file either.
Seems like the maven-compiler-plugin
includes the -g
option by default. And this allows Spring to retrieve parameter names at runtime.
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