What is the difference b / w @RequestParam and @QueryParam Anotation

What is the main difference between @RequestParam and @QueryParam in Spring MVC?

+16
source share
4 answers

Besides the differences in structure mentioned, one of the main differences is @RequestParam which will always wait for a value to be bound. Therefore, if the value is not passed, it will throw an error. This is not true in @QueryParam

To explicitly have the @RequestParam parameter, use required = false when using @RequestParam

+9
source

@QueryParam is a JAX-RS annotation, and @RequestParam is Spring .

+36
source

In addition to the above, I noticed one more difference: QueryParam does not translate parameter values ​​to List type, as RequestParam does.

I mean that "@RequestParam (" para ") List param is able to convert parameter values ​​separated by commas to List, @QueryParam cannot convert this.

0
source

I will try to shed more detailed light on this issue.

Let's start with the semantics " Query Parameter vs. Query Parameter" , respectively @QueryParam and @RequestParam

The request parameter in accordance with the HTTP specification, as well as in accordance with the JAX-RS implementation of this annotation ( @QueryParam ) represents the parameter found in the query string. those. The string that follows the question mark in the full query URL.

The request parameter in accordance with the HTTP specification, as well as in accordance with the implementation of this annotation ( @RequestParam ) Spring MVC (org.springframework.web.bind.annotation) represents the HTTP request parameter, and to be clear here, it does not indicate what type of parameter is it - request, header, body, etc.


Now let's talk about the implementation itself.

@QueryParam

@QueryParam refers to JAX-RS , which states :

@QueryParam Binds the value (s) of an HTTP request parameter to a resource method parameter, a resource class field, or a property of a component of a resource class

Note that this is a query parameter that is bound to the parameter of the resource method (that is, the parameter in the query string).

@RequestParam

@RequestParam on the other hand, is owned by the Spring Framework, which claims that:

The @RequestParam annotation indicates that the method parameter should be associated with the web request parameter.
• In Spring MVC, “query parameters” are mapped to query parameters, form data, and details in multicomponent queries. This is due to the fact that the Servlet API combines the request parameters and form data into one card called “parameters”, which includes an automatic analysis of the request body.


Therefore, I think that names can really be a little misleading, but after you delve into them a little more, they speak for themselves. Just try to stick to one when you only need a query parameter, and the other when you need any query parameter.
0
source

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


All Articles