Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Spring match query parameter only by formal parameter name?

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)

like image 620
gabi197 Avatar asked Jun 03 '18 09:06

gabi197


People also ask

Is request parameter same as query parameter?

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.

What does @RequestParam do in spring?

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.

What is query parameter in Java?

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.

What is Spring <UNK>QueryParam?

@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)


1 Answers

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.

like image 88
Oleksandr Pyrohov Avatar answered Sep 27 '22 18:09

Oleksandr Pyrohov